Pierber commited on
Commit
5013a5a
·
verified ·
1 Parent(s): 216714f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -53
app.py CHANGED
@@ -2,66 +2,27 @@ import gradio as gr
2
  import os
3
  from groq import Groq
4
 
5
- # 1. RECUPERO LA CHIAVE DAI SEGRETI
6
  api_key = os.environ.get("GROQ_API_KEY")
7
 
8
- # 2. DEFINISCO LA FUNZIONE DI RISPOSTA
9
- def respond(strategia_docente, chat_history, user_input):
10
- # Controllo di sicurezza
11
  if not api_key:
12
- yield "⚠️ ERRORE: Non trovo la chiave API. Vai in Settings > Variables and secrets e controlla di aver creato 'GROQ_API_KEY'."
13
- return
14
-
15
  try:
16
- # Mi connetto a Groq
17
  client = Groq(api_key=api_key)
18
-
19
- # System Prompt
20
- system_prompt = f"""
21
- Sei Marco, uno studente di 14 anni.
22
- Il tuo docente sta usando questa strategia: "{strategia_docente}".
23
- Reagisci di conseguenza. Sii breve e colloquiale.
24
- """
25
-
26
- # Costruisco la memoria
27
- messages = [{"role": "system", "content": system_prompt}]
28
- for val in chat_history:
29
- if val[0]: messages.append({"role": "user", "content": val[0]})
30
- if val[1]: messages.append({"role": "assistant", "content": val[1]})
31
- messages.append({"role": "user", "content": user_input})
32
-
33
- # CHIAMATA AL NUOVO MODELLO (Quello vecchio dava errore!)
34
  completion = client.chat.completions.create(
35
- model="llama-3.3-70b-versatile", # <--- ECCO LA CORREZIONE
36
- messages=messages,
37
- temperature=0.7,
38
- max_tokens=150,
39
- stream=True
 
40
  )
41
-
42
- response = ""
43
- for chunk in completion:
44
- token = chunk.choices[0].delta.content
45
- if token:
46
- response += token
47
- yield response
48
-
49
  except Exception as e:
50
- # Se c'è un errore, lo scriviamo nella chat invece di fare il popup rosso
51
- yield f"⛔ ERRORE TECNICO: {str(e)}"
52
-
53
- # 3. INTERFACCIA GRAFICA
54
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
55
- gr.Markdown("# 🏫 Project Janus: Laboratorio")
56
- with gr.Row():
57
- strategia = gr.Textbox(label="Strategia Avatar", value="Sono autoritario")
58
- chatbot = gr.Chatbot(label="Chat con Marco", height=350)
59
- msg = gr.Textbox(label="Messaggio", placeholder="Scrivi a Marco...")
60
- clear = gr.Button("Reset")
61
-
62
- msg.submit(lambda x, y: ("", y + [[x, None]]), [msg, chatbot], [msg, chatbot], queue=False).then(
63
- respond, [strategia, chatbot], chatbot
64
- )
65
- clear.click(lambda: None, None, chatbot, queue=False)
66
 
 
 
67
  demo.launch()
 
2
  import os
3
  from groq import Groq
4
 
5
+ # Recupero la chiave
6
  api_key = os.environ.get("GROQ_API_KEY")
7
 
8
+ def chat_con_marco(messaggio, storia):
 
 
9
  if not api_key:
10
+ return "ERRORE: Chiave API mancante nei Settings."
11
+
 
12
  try:
 
13
  client = Groq(api_key=api_key)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  completion = client.chat.completions.create(
15
+ model="llama-3.3-70b-versatile",
16
+ messages=[
17
+ {"role": "system", "content": "Sei Marco, studente ribelle."},
18
+ {"role": "user", "content": messaggio}
19
+ ],
20
+ temperature=0.7
21
  )
22
+ return completion.choices[0].message.content
 
 
 
 
 
 
 
23
  except Exception as e:
24
+ return f"ERRORE GROQ: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
+ # Interfaccia ultra-semplice
27
+ demo = gr.ChatInterface(fn=chat_con_marco, title="Test Project Janus")
28
  demo.launch()