Pierber commited on
Commit
6dbc00a
·
verified ·
1 Parent(s): beae6de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -31
app.py CHANGED
@@ -5,19 +5,20 @@ import os
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.
@@ -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
- # 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,
48
- temperature=0.8,
49
- max_tokens=150
 
50
  )
51
 
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__":
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()