Update app.py
Browse files
app.py
CHANGED
|
@@ -3,15 +3,15 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# --- TOKEN ---
|
| 6 |
-
# Assicurati che HF_TOKEN sia nei Secrets
|
| 7 |
token = os.environ.get("HF_TOKEN")
|
| 8 |
|
| 9 |
# USIAMO QWEN 2.5 7B (Modello Cinese di Alibaba)
|
| 10 |
-
# È
|
| 11 |
-
# Questo riduce drasticamente il rischio di Timeout (Errore 0).
|
| 12 |
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=token)
|
| 13 |
|
| 14 |
def rispondi_a_marco(messaggio, storia):
|
|
|
|
| 15 |
system_prompt = """
|
| 16 |
SEI MARCO, 14 ANNI.
|
| 17 |
Sei uno studente svogliato, sarcastico e ribelle.
|
|
@@ -22,7 +22,7 @@ def rispondi_a_marco(messaggio, storia):
|
|
| 22 |
|
| 23 |
messages = [{"role": "system", "content": system_prompt}]
|
| 24 |
|
| 25 |
-
# Memoria
|
| 26 |
for turno in storia:
|
| 27 |
if turno[0] and turno[1]:
|
| 28 |
messages.append({"role": "user", "content": str(turno[0])})
|
|
@@ -31,9 +31,7 @@ def rispondi_a_marco(messaggio, storia):
|
|
| 31 |
messages.append({"role": "user", "content": messaggio})
|
| 32 |
|
| 33 |
try:
|
| 34 |
-
# ---
|
| 35 |
-
# Qwen 7B è così veloce che non serve lo streaming.
|
| 36 |
-
# Chiediamo la risposta completa subito. È molto più stabile.
|
| 37 |
risposta = client.chat_completion(
|
| 38 |
messages,
|
| 39 |
max_tokens=150,
|
|
@@ -44,14 +42,14 @@ def rispondi_a_marco(messaggio, storia):
|
|
| 44 |
return risposta.choices[0].message.content
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
-
return f"⚠️ ERRORE: {str(e)}
|
| 48 |
|
| 49 |
-
# Interfaccia
|
| 50 |
demo = gr.ChatInterface(
|
| 51 |
fn=rispondi_a_marco,
|
| 52 |
title="Project Janus: Marco (Qwen 7B)",
|
| 53 |
-
description="Motore: Qwen 2.5 (Alibaba).
|
| 54 |
-
theme
|
| 55 |
)
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# --- TOKEN ---
|
| 6 |
+
# Assicurati che HF_TOKEN sia nei Secrets.
|
| 7 |
token = os.environ.get("HF_TOKEN")
|
| 8 |
|
| 9 |
# USIAMO QWEN 2.5 7B (Modello Cinese di Alibaba)
|
| 10 |
+
# È veloce, stabile e parla italiano.
|
|
|
|
| 11 |
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=token)
|
| 12 |
|
| 13 |
def rispondi_a_marco(messaggio, storia):
|
| 14 |
+
# Prompt Pedagogico
|
| 15 |
system_prompt = """
|
| 16 |
SEI MARCO, 14 ANNI.
|
| 17 |
Sei uno studente svogliato, sarcastico e ribelle.
|
|
|
|
| 22 |
|
| 23 |
messages = [{"role": "system", "content": system_prompt}]
|
| 24 |
|
| 25 |
+
# Costruzione Memoria
|
| 26 |
for turno in storia:
|
| 27 |
if turno[0] and turno[1]:
|
| 28 |
messages.append({"role": "user", "content": str(turno[0])})
|
|
|
|
| 31 |
messages.append({"role": "user", "content": messaggio})
|
| 32 |
|
| 33 |
try:
|
| 34 |
+
# --- NO STREAMING (Per evitare errori di connessione) ---
|
|
|
|
|
|
|
| 35 |
risposta = client.chat_completion(
|
| 36 |
messages,
|
| 37 |
max_tokens=150,
|
|
|
|
| 42 |
return risposta.choices[0].message.content
|
| 43 |
|
| 44 |
except Exception as e:
|
| 45 |
+
return f"⚠️ ERRORE: {str(e)}"
|
| 46 |
|
| 47 |
+
# Interfaccia (PULITA, SENZA TEMA)
|
| 48 |
demo = gr.ChatInterface(
|
| 49 |
fn=rispondi_a_marco,
|
| 50 |
title="Project Janus: Marco (Qwen 7B)",
|
| 51 |
+
description="Motore: Qwen 2.5 (Alibaba). Stabile e Veloce."
|
| 52 |
+
# Ho rimosso 'theme' per evitare il crash!
|
| 53 |
)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|