Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,67 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
|
|
|
|
| 4 |
MODEL_ID = "INTERX/Qwen2.5-GenX-7B"
|
| 5 |
|
|
|
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 7 |
-
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
tokenize=True,
|
| 14 |
add_generation_prompt=True,
|
| 15 |
-
return_tensors=
|
| 16 |
).to(model.device)
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 20 |
-
return response
|
| 21 |
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
|
| 4 |
+
# ID del modello Hugging Face (puoi cambiarlo con un altro compatibile)
|
| 5 |
MODEL_ID = "INTERX/Qwen2.5-GenX-7B"
|
| 6 |
|
| 7 |
+
# Caricamento tokenizer e modello
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
MODEL_ID,
|
| 11 |
+
device_map="auto",
|
| 12 |
+
torch_dtype="auto",
|
| 13 |
+
trust_remote_code=True
|
| 14 |
+
)
|
| 15 |
|
| 16 |
+
# Funzione di chat con storico
|
| 17 |
+
def chat(user_input, chat_history):
|
| 18 |
+
if chat_history is None:
|
| 19 |
+
chat_history = []
|
| 20 |
+
|
| 21 |
+
# Aggiunge il nuovo input allo storico
|
| 22 |
+
chat_history.append({"role": "user", "content": user_input})
|
| 23 |
+
|
| 24 |
+
# Prepara l'input per il modello
|
| 25 |
+
tokenized_input = tokenizer.apply_chat_template(
|
| 26 |
+
chat_history,
|
| 27 |
tokenize=True,
|
| 28 |
add_generation_prompt=True,
|
| 29 |
+
return_tensors='pt'
|
| 30 |
).to(model.device)
|
| 31 |
|
| 32 |
+
# Genera la risposta
|
| 33 |
+
output = model.generate(tokenized_input, max_new_tokens=512, do_sample=True, temperature=0.7)
|
| 34 |
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
|
|
|
| 35 |
|
| 36 |
+
# Pulisce eventuali ripetizioni del prompt
|
| 37 |
+
response = response.split("user:")[-1].split("assistant:")[-1].strip()
|
| 38 |
+
|
| 39 |
+
# Aggiunge la risposta allo storico
|
| 40 |
+
chat_history.append({"role": "assistant", "content": response})
|
| 41 |
+
|
| 42 |
+
# Prepara lo storico per il componente gr.Chatbot
|
| 43 |
+
gr_history = [[entry["content"], None] if entry["role"] == "user" else [None, entry["content"]] for entry in chat_history]
|
| 44 |
+
merged_history = []
|
| 45 |
+
for i in range(0, len(gr_history), 2):
|
| 46 |
+
user_msg = gr_history[i][0] if i < len(gr_history) else ""
|
| 47 |
+
ai_msg = gr_history[i+1][1] if i+1 < len(gr_history) else ""
|
| 48 |
+
merged_history.append([user_msg, ai_msg])
|
| 49 |
+
|
| 50 |
+
return merged_history, chat_history
|
| 51 |
+
|
| 52 |
+
# UI con Gradio
|
| 53 |
+
with gr.Blocks(title="Chat Qwen2.5-GenX-7B") as demo:
|
| 54 |
+
gr.Markdown("# π¬ Chat con Qwen2.5-GenX-7B")
|
| 55 |
+
chatbot = gr.Chatbot(label="Conversazione")
|
| 56 |
+
state = gr.State([]) # storico della conversazione
|
| 57 |
+
with gr.Row():
|
| 58 |
+
txt = gr.Textbox(show_label=False, placeholder="Scrivi un messaggio...").style(container=False)
|
| 59 |
+
send_btn = gr.Button("Invia")
|
| 60 |
+
clear_btn = gr.Button("β Reset")
|
| 61 |
+
|
| 62 |
+
# Azioni
|
| 63 |
+
send_btn.click(chat, [txt, state], [chatbot, state])
|
| 64 |
+
txt.submit(chat, [txt, state], [chatbot, state])
|
| 65 |
+
clear_btn.click(lambda: ([], []), None, [chatbot, state])
|
| 66 |
+
|
| 67 |
+
demo.launch()
|