Update app.py
Browse files
app.py
CHANGED
|
@@ -5,19 +5,20 @@ import os
|
|
| 5 |
# --- TUA CHIAVE API ---
|
| 6 |
api_key = "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"
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
try:
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
|
| 17 |
-
# 2. PROMPT
|
| 18 |
system_prompt = f"""
|
| 19 |
SEI MARCO, 14 ANNI.
|
| 20 |
-
SITUAZIONE: Il docente usa
|
| 21 |
|
| 22 |
REGOLE:
|
| 23 |
- Sei svogliato, sarcastico e breve.
|
|
@@ -26,45 +27,45 @@ def rispondi_a_marco(messaggio, storia, strategia_docente):
|
|
| 26 |
- Rispondi in massimo 2 frasi.
|
| 27 |
"""
|
| 28 |
|
| 29 |
-
# 3. MEMORIA (Ricostruita da zero ogni volta)
|
| 30 |
messages = [{"role": "system", "content": system_prompt}]
|
| 31 |
|
| 32 |
-
#
|
| 33 |
for turno in storia:
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
messages.append({"role": "
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# Aggiungiamo il messaggio NUOVO
|
| 42 |
messages.append({"role": "user", "content": messaggio})
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
model="llama-3.3-70b-versatile",
|
| 47 |
messages=messages,
|
| 48 |
-
temperature=0.8,
|
| 49 |
-
max_tokens=150
|
|
|
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
-
|
| 56 |
-
return f"⛔ ERRORE: {str(e)}"
|
| 57 |
|
| 58 |
# --- INTERFACCIA ---
|
| 59 |
-
#
|
| 60 |
interfaccia = gr.ChatInterface(
|
| 61 |
fn=rispondi_a_marco,
|
| 62 |
-
additional_inputs=[
|
| 63 |
-
gr.Textbox(label="Strategia Didattica", value="Sono autoritario")
|
| 64 |
-
],
|
| 65 |
title="🏫 Project Janus",
|
| 66 |
-
description="
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
interfaccia.launch()
|
|
|
|
| 5 |
# --- TUA CHIAVE API ---
|
| 6 |
api_key = "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"
|
| 7 |
|
| 8 |
+
# Creiamo il client
|
| 9 |
+
client = Groq(api_key=api_key)
|
| 10 |
+
|
| 11 |
+
def rispondi_a_marco(messaggio, storia):
|
| 12 |
+
# La funzione ora accetta solo messaggio e storia direttamente
|
| 13 |
+
# (La strategia la mettiamo fissa nel prompt per semplificare ed evitare errori)
|
| 14 |
|
| 15 |
try:
|
| 16 |
+
# 1. Prompt "Cattivo"
|
| 17 |
+
strategia = "Sono un docente autoritario e severo" # Puoi cambiarlo qui se vuoi
|
| 18 |
|
|
|
|
| 19 |
system_prompt = f"""
|
| 20 |
SEI MARCO, 14 ANNI.
|
| 21 |
+
SITUAZIONE: Il docente usa questa strategia: "{strategia}".
|
| 22 |
|
| 23 |
REGOLE:
|
| 24 |
- Sei svogliato, sarcastico e breve.
|
|
|
|
| 27 |
- Rispondi in massimo 2 frasi.
|
| 28 |
"""
|
| 29 |
|
|
|
|
| 30 |
messages = [{"role": "system", "content": system_prompt}]
|
| 31 |
|
| 32 |
+
# 2. Gestione Storia (Blindata)
|
| 33 |
for turno in storia:
|
| 34 |
+
utente = turno[0]
|
| 35 |
+
bot = turno[1]
|
| 36 |
+
if utente is not None and bot is not None:
|
| 37 |
+
messages.append({"role": "user", "content": str(utente)})
|
| 38 |
+
messages.append({"role": "assistant", "content": str(bot)})
|
| 39 |
+
|
|
|
|
|
|
|
| 40 |
messages.append({"role": "user", "content": messaggio})
|
| 41 |
|
| 42 |
+
# 3. CHIAMATA STREAMING (LA SOLUZIONE ALL'ERRORE 0)
|
| 43 |
+
stream = client.chat.completions.create(
|
| 44 |
model="llama-3.3-70b-versatile",
|
| 45 |
messages=messages,
|
| 46 |
+
temperature=0.8,
|
| 47 |
+
max_tokens=150,
|
| 48 |
+
stream=True # <--- QUESTO TIENE VIVA LA CONNESSIONE
|
| 49 |
)
|
| 50 |
|
| 51 |
+
partial_response = ""
|
| 52 |
+
for chunk in stream:
|
| 53 |
+
token = chunk.choices[0].delta.content
|
| 54 |
+
if token:
|
| 55 |
+
partial_response += token
|
| 56 |
+
yield partial_response # <--- Invia pezzettino per pezzettino
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
+
yield f"⛔ ERRORE: {str(e)}"
|
|
|
|
| 60 |
|
| 61 |
# --- INTERFACCIA ---
|
| 62 |
+
# ChatInterface gestisce lo streaming automaticamente
|
| 63 |
interfaccia = gr.ChatInterface(
|
| 64 |
fn=rispondi_a_marco,
|
|
|
|
|
|
|
|
|
|
| 65 |
title="🏫 Project Janus",
|
| 66 |
+
description="Parla con Marco. Se vedi scrivere '...', sta pensando.",
|
| 67 |
+
examples=["Buongiorno Marco, siediti.", "Perché non hai fatto i compiti?", "Esci dalla classe!"]
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
+
interfaccia.launch()
|