Update app.py
Browse files
app.py
CHANGED
|
@@ -3,44 +3,45 @@ from groq import Groq
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# --- TUA CHIAVE API ---
|
| 6 |
-
# (La chiave è corretta, l'abbiamo visto funzionare)
|
| 7 |
api_key = "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"
|
| 8 |
|
| 9 |
-
# Creiamo il client UNA SOLA VOLTA all'inizio (più stabile)
|
| 10 |
-
client = Groq(api_key=api_key)
|
| 11 |
-
|
| 12 |
def rispondi_a_marco(messaggio, storia, strategia_docente):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
-
#
|
|
|
|
|
|
|
|
|
|
| 15 |
system_prompt = f"""
|
| 16 |
-
SEI MARCO, 14 ANNI.
|
| 17 |
-
SITUAZIONE: Il
|
| 18 |
|
| 19 |
REGOLE:
|
| 20 |
-
- Sei svogliato,
|
| 21 |
-
-
|
| 22 |
-
-
|
| 23 |
-
- Non dire MAI "Come posso aiutarti?".
|
| 24 |
- Rispondi in massimo 2 frasi.
|
| 25 |
"""
|
| 26 |
|
| 27 |
-
#
|
| 28 |
messages = [{"role": "system", "content": system_prompt}]
|
| 29 |
|
| 30 |
-
#
|
| 31 |
for turno in storia:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
messages.append({"role": "
|
| 38 |
-
messages.append({"role": "assistant", "content": str(bot_msg)})
|
| 39 |
|
| 40 |
-
# Aggiungiamo il messaggio
|
| 41 |
messages.append({"role": "user", "content": messaggio})
|
| 42 |
|
| 43 |
-
#
|
| 44 |
completion = client.chat.completions.create(
|
| 45 |
model="llama-3.3-70b-versatile",
|
| 46 |
messages=messages,
|
|
@@ -51,18 +52,18 @@ def rispondi_a_marco(messaggio, storia, strategia_docente):
|
|
| 51 |
return completion.choices[0].message.content
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
-
# Se
|
| 55 |
-
return f"⛔ ERRORE: {str(e)}
|
| 56 |
|
| 57 |
# --- INTERFACCIA ---
|
|
|
|
| 58 |
interfaccia = gr.ChatInterface(
|
| 59 |
fn=rispondi_a_marco,
|
| 60 |
additional_inputs=[
|
| 61 |
-
gr.Textbox(label="
|
| 62 |
],
|
| 63 |
-
title="🏫 Project Janus
|
| 64 |
-
description="
|
| 65 |
-
# Rimosso il tema che dava problemi
|
| 66 |
)
|
| 67 |
|
| 68 |
if __name__ == "__main__":
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# --- TUA CHIAVE API ---
|
|
|
|
| 6 |
api_key = "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"
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
def rispondi_a_marco(messaggio, storia, strategia_docente):
|
| 9 |
+
# 1. INIT CLIENT (Lo facciamo QUI, ogni volta, per evitare che la connessione cada)
|
| 10 |
+
if not api_key:
|
| 11 |
+
return "⚠️ ERRORE: Chiave mancante."
|
| 12 |
+
|
| 13 |
try:
|
| 14 |
+
# Creiamo una connessione fresca per questo singolo messaggio
|
| 15 |
+
client = Groq(api_key=api_key)
|
| 16 |
+
|
| 17 |
+
# 2. PROMPT
|
| 18 |
system_prompt = f"""
|
| 19 |
+
SEI MARCO, 14 ANNI.
|
| 20 |
+
SITUAZIONE: Il docente usa la strategia: "{strategia_docente}".
|
| 21 |
|
| 22 |
REGOLE:
|
| 23 |
+
- Sei svogliato, sarcastico e breve.
|
| 24 |
+
- Se il docente fa il "prof", tu sbuffa.
|
| 25 |
+
- Non dire MAI "Come posso aiutarti".
|
|
|
|
| 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 |
+
# Aggiungiamo la storia precedente
|
| 33 |
for turno in storia:
|
| 34 |
+
u_msg = turno[0]
|
| 35 |
+
b_msg = turno[1]
|
| 36 |
+
# Controllo rigido: aggiungiamo solo se ci sono testi validi
|
| 37 |
+
if u_msg and b_msg:
|
| 38 |
+
messages.append({"role": "user", "content": str(u_msg)})
|
| 39 |
+
messages.append({"role": "assistant", "content": str(b_msg)})
|
|
|
|
| 40 |
|
| 41 |
+
# Aggiungiamo il messaggio NUOVO
|
| 42 |
messages.append({"role": "user", "content": messaggio})
|
| 43 |
|
| 44 |
+
# 4. CHIAMATA API
|
| 45 |
completion = client.chat.completions.create(
|
| 46 |
model="llama-3.3-70b-versatile",
|
| 47 |
messages=messages,
|
|
|
|
| 52 |
return completion.choices[0].message.content
|
| 53 |
|
| 54 |
except Exception as e:
|
| 55 |
+
# Se fallisce, ci dice esattamente perché
|
| 56 |
+
return f"⛔ ERRORE: {str(e)}"
|
| 57 |
|
| 58 |
# --- INTERFACCIA ---
|
| 59 |
+
# Usiamo ChatInterface semplice, senza fronzoli grafici che possono rompere
|
| 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="Prova a dialogare con Marco. Se si blocca, ricarica la pagina."
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|