Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,46 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
|
5 |
+
# Load model and tokenizer - specify the correct model class
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained("anthracite-org/magnum-v4-12b-gguf")
|
7 |
+
model = AutoModelForCausalLM.from_pretrained("anthracite-org/magnum-v4-12b-gguf", torch_dtype=torch.float16, device_map="auto") # Adjust dtype and device_map as needed
|
8 |
+
|
9 |
+
|
10 |
+
def generate_text(prompt, max_length=50, num_return_sequences=1, temperature=0.7, top_p=0.95, top_k=50):
|
11 |
+
try:
|
12 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device) # Ensure inputs are on the same device as the model
|
13 |
+
|
14 |
+
outputs = model.generate(
|
15 |
+
**inputs,
|
16 |
+
max_new_tokens=max_length, # Use max_new_tokens instead of max_length with Transformers >= 4.30
|
17 |
+
do_sample=True,
|
18 |
+
temperature=temperature,
|
19 |
+
top_p=top_p,
|
20 |
+
top_k=top_k,
|
21 |
+
num_return_sequences=num_return_sequences
|
22 |
+
)
|
23 |
+
|
24 |
+
generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
25 |
+
return "\n\n".join(generated_text)
|
26 |
+
|
27 |
+
except Exception as e: # Handle potential errors
|
28 |
+
return f"Error: {e}"
|
29 |
+
|
30 |
+
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=generate_text,
|
33 |
+
inputs=[
|
34 |
+
gr.Textbox(lines=2, placeholder="Enter a prompt...", label="Prompt"),
|
35 |
+
gr.Slider(50, 200, value=100, label="Max Length"),
|
36 |
+
gr.Slider(0, 1, value=0.7, label="Temperature"),
|
37 |
+
gr.Slider(0, 1, value=0.95, label="Top p"),
|
38 |
+
gr.Slider(0, 50, value=50, step=1, label="Top k"),
|
39 |
+
|
40 |
+
],
|
41 |
+
outputs="text",
|
42 |
+
title="Text Generation with magnum-v4-12b-gguf",
|
43 |
+
description="Generate text using the magnum model. Please be patient, as generation can take time."
|
44 |
+
)
|
45 |
+
|
46 |
+
iface.launch(share=True) # Consider adding share=True for others to try
|