Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# ---
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
def rispondi_a_marco(messaggio, storia):
|
| 9 |
-
#
|
| 10 |
-
# Se lo mettiamo fuori, la connessione cade dopo il primo turno!
|
| 11 |
-
client = Groq(api_key=api_key)
|
| 12 |
-
|
| 13 |
-
# 2. Prompt
|
| 14 |
system_prompt = """
|
| 15 |
SEI MARCO, 14 ANNI.
|
| 16 |
-
Sei
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
"""
|
| 19 |
|
| 20 |
-
# 3. Costruiamo la memoria pulita
|
| 21 |
messages = [{"role": "system", "content": system_prompt}]
|
| 22 |
|
| 23 |
-
#
|
| 24 |
for turno in storia:
|
| 25 |
-
if turno[0] and turno[1]:
|
| 26 |
messages.append({"role": "user", "content": str(turno[0])})
|
| 27 |
messages.append({"role": "assistant", "content": str(turno[1])})
|
| 28 |
|
| 29 |
-
# Aggiungiamo il messaggio nuovo
|
| 30 |
messages.append({"role": "user", "content": messaggio})
|
| 31 |
|
| 32 |
try:
|
| 33 |
-
#
|
| 34 |
-
stream = client.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
stream=True
|
| 40 |
)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
parola_parziale = ""
|
| 44 |
for chunk in stream:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
-
yield f"⚠️ ERRORE: {str(e)}"
|
| 52 |
|
| 53 |
# Interfaccia
|
| 54 |
demo = gr.ChatInterface(
|
| 55 |
-
fn=rispondi_a_marco,
|
| 56 |
-
title="Project Janus: Marco",
|
| 57 |
-
description="
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# --- RECUPERO IL TOKEN E IMPORTO IL MODELLO ---
|
| 6 |
+
# Usiamo Qwen 2.5 72B, che è potentissimo e ospitato direttamente da HF
|
| 7 |
+
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct", token=os.environ.get("HF_TOKEN"))
|
| 8 |
|
| 9 |
def rispondi_a_marco(messaggio, storia):
|
| 10 |
+
# Prompt Pedagogico
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
system_prompt = """
|
| 12 |
SEI MARCO, 14 ANNI.
|
| 13 |
+
Sei in una scuola tecnica italiana.
|
| 14 |
+
Atteggiamento: Svogliato, ironico, un po' ribelle ma non maleducato.
|
| 15 |
+
Obiettivo: Evitare interrogazioni e compiti.
|
| 16 |
+
Usa slang giovanile ("Boh", "Raga", "Che ansia").
|
| 17 |
+
Non fare mai l'assistente utile.
|
| 18 |
"""
|
| 19 |
|
|
|
|
| 20 |
messages = [{"role": "system", "content": system_prompt}]
|
| 21 |
|
| 22 |
+
# Ricostruiamo la memoria
|
| 23 |
for turno in storia:
|
| 24 |
+
if turno[0] and turno[1]:
|
| 25 |
messages.append({"role": "user", "content": str(turno[0])})
|
| 26 |
messages.append({"role": "assistant", "content": str(turno[1])})
|
| 27 |
|
|
|
|
| 28 |
messages.append({"role": "user", "content": messaggio})
|
| 29 |
|
| 30 |
try:
|
| 31 |
+
# Chiamata Streaming Nativa
|
| 32 |
+
stream = client.chat_completion(
|
| 33 |
+
messages,
|
| 34 |
+
max_tokens=150,
|
| 35 |
+
stream=True,
|
| 36 |
+
temperature=0.8
|
|
|
|
| 37 |
)
|
| 38 |
|
| 39 |
+
output = ""
|
|
|
|
| 40 |
for chunk in stream:
|
| 41 |
+
# La struttura qui è leggermente diversa da Groq
|
| 42 |
+
content = chunk.choices[0].delta.content
|
| 43 |
+
if content:
|
| 44 |
+
output += content
|
| 45 |
+
yield output
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
+
yield f"⚠️ ERRORE HF: {str(e)}"
|
| 49 |
|
| 50 |
# Interfaccia
|
| 51 |
demo = gr.ChatInterface(
|
| 52 |
+
fn=rispondi_a_marco,
|
| 53 |
+
title="🏫 Project Janus: Marco (HF Native)",
|
| 54 |
+
description="Versione stabile su server Hugging Face."
|
| 55 |
)
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|