Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,48 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
# Carica il modello e il tokenizer
|
| 5 |
+
model_name = "Qwen/Qwen3-235B-A22B"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto")
|
| 8 |
+
|
| 9 |
+
def generate_text(prompt, max_length=200, temperature=0.7):
|
| 10 |
+
# Tokenizza l'input
|
| 11 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 12 |
+
|
| 13 |
+
# Genera la risposta
|
| 14 |
+
outputs = model.generate(
|
| 15 |
+
inputs["input_ids"],
|
| 16 |
+
max_length=max_length,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
temperature=temperature,
|
| 18 |
+
do_sample=True,
|
| 19 |
+
top_p=0.9,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Decodifica la risposta
|
| 23 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
+
return response
|
| 25 |
+
|
| 26 |
+
# Crea l'interfaccia Gradio
|
| 27 |
+
with gr.Blocks() as demo:
|
| 28 |
+
gr.Markdown("# Qwen3-235B Demo")
|
| 29 |
+
|
| 30 |
+
with gr.Row():
|
| 31 |
+
with gr.Column():
|
| 32 |
+
prompt_input = gr.Textbox(label="Il tuo prompt", lines=4)
|
| 33 |
+
temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="Temperatura")
|
| 34 |
+
max_length = gr.Slider(minimum=50, maximum=500, value=200, step=10, label="Lunghezza massima")
|
| 35 |
+
submit_btn = gr.Button("Genera")
|
| 36 |
+
|
| 37 |
+
with gr.Column():
|
| 38 |
+
output = gr.Textbox(label="Risposta generata", lines=8)
|
| 39 |
+
|
| 40 |
+
submit_btn.click(
|
| 41 |
+
generate_text,
|
| 42 |
+
inputs=[prompt_input, max_length, temperature],
|
| 43 |
+
outputs=[output]
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Avvia l'applicazione
|
| 47 |
if __name__ == "__main__":
|
| 48 |
+
demo.launch()
|