Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
# CONFIGURAZIONE
|
| 6 |
API_KEY = "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"
|
|
|
|
| 7 |
|
| 8 |
def rispondi_a_marco(messaggio, storia):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
-
# Client Groq
|
| 11 |
-
client = OpenAI(
|
| 12 |
-
api_key=API_KEY,
|
| 13 |
-
base_url="https://api.groq.com/openai/v1"
|
| 14 |
-
)
|
| 15 |
-
|
| 16 |
-
# Prompt
|
| 17 |
-
system_prompt = """
|
| 18 |
-
SEI MARCO, 14 ANNI.
|
| 19 |
-
Sei uno studente svogliato, ribelle e sarcastico.
|
| 20 |
-
Odi la scuola. Rispondi male ai professori.
|
| 21 |
-
Usa slang ("Boh", "Che ansia", "Raga", "Cringe").
|
| 22 |
-
Rispondi in massimo 2 frasi.
|
| 23 |
-
"""
|
| 24 |
-
|
| 25 |
-
# Memoria
|
| 26 |
-
messages = [{"role": "system", "content": system_prompt}]
|
| 27 |
-
|
| 28 |
for turno in storia:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
stream=True,
|
| 42 |
-
timeout=60.0 # <--- ECCO LA MODIFICA: Aspetta fino a 60 secondi
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
risposta_parziale = ""
|
| 46 |
-
for chunk in stream:
|
| 47 |
-
if chunk.choices[0].delta.content:
|
| 48 |
-
risposta_parziale += chunk.choices[0].delta.content
|
| 49 |
-
yield risposta_parziale
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
-
|
| 53 |
|
| 54 |
-
#
|
|
|
|
| 55 |
demo = gr.ChatInterface(
|
| 56 |
fn=rispondi_a_marco,
|
| 57 |
-
title="Project Janus: Marco (
|
| 58 |
-
description="
|
| 59 |
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
+
# --- CONFIGURAZIONE ---
|
| 6 |
API_KEY = "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"
|
| 7 |
+
URL = "https://api.groq.com/openai/v1/chat/completions"
|
| 8 |
|
| 9 |
def rispondi_a_marco(messaggio, storia):
|
| 10 |
+
# 1. Prompt
|
| 11 |
+
system_prompt = """
|
| 12 |
+
SEI MARCO, 14 ANNI.
|
| 13 |
+
Sei uno studente svogliato, ribelle e sarcastico.
|
| 14 |
+
Odi la scuola. Rispondi male ai professori.
|
| 15 |
+
Usa slang ("Boh", "Che ansia", "Raga", "Cringe").
|
| 16 |
+
Rispondi in massimo 2 frasi.
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
# Inizializziamo i messaggi con il sistema
|
| 20 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 21 |
+
|
| 22 |
+
# --- BLOCCO DI SICUREZZA MEMORIA ---
|
| 23 |
+
# Questo ciclo 'try-except' impedisce il crash se la storia è formattata male
|
| 24 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
for turno in storia:
|
| 26 |
+
# Controllo rigoroso: accettiamo solo se sono stringhe valide
|
| 27 |
+
if isinstance(turno, (list, tuple)) and len(turno) == 2:
|
| 28 |
+
user_msg = str(turno[0]) if turno[0] is not None else ""
|
| 29 |
+
bot_msg = str(turno[1]) if turno[1] is not None else ""
|
| 30 |
+
|
| 31 |
+
if user_msg and bot_msg:
|
| 32 |
+
messages.append({"role": "user", "content": user_msg})
|
| 33 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 34 |
+
except Exception as e:
|
| 35 |
+
# Se c'è un errore nella memoria, lo stampiamo ma NON FERMIAMO IL PROGRAMMA
|
| 36 |
+
print(f"Errore lettura memoria: {e}")
|
| 37 |
+
# Continuiamo senza memoria vecchia, meglio smemorato che morto.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# Aggiungiamo il messaggio nuovo
|
| 40 |
+
messages.append({"role": "user", "content": str(messaggio)})
|
| 41 |
+
|
| 42 |
+
# 2. Configurazione Dati (Payload)
|
| 43 |
+
payload = {
|
| 44 |
+
"model": "llama-3.1-8b-instant", # Modello Veloce
|
| 45 |
+
"messages": messages,
|
| 46 |
+
"temperature": 0.8,
|
| 47 |
+
"max_tokens": 150,
|
| 48 |
+
"stream": False # NO STREAMING (per evitare crash socket)
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
headers = {
|
| 52 |
+
"Content-Type": "application/json",
|
| 53 |
+
"Authorization": f"Bearer {API_KEY}"
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
# 3. Chiamata Diretta (Blindata)
|
| 57 |
+
try:
|
| 58 |
+
response = requests.post(URL, headers=headers, json=payload, timeout=20)
|
| 59 |
+
|
| 60 |
+
if response.status_code == 200:
|
| 61 |
+
data = response.json()
|
| 62 |
+
return data['choices'][0]['message']['content']
|
| 63 |
+
else:
|
| 64 |
+
return f"⚠️ ERRORE API ({response.status_code}): {response.text}"
|
| 65 |
+
|
| 66 |
except Exception as e:
|
| 67 |
+
return f"⚠️ ERRORE CRITICO: {str(e)}"
|
| 68 |
|
| 69 |
+
# --- INTERFACCIA ---
|
| 70 |
+
# Semplice e pulita
|
| 71 |
demo = gr.ChatInterface(
|
| 72 |
fn=rispondi_a_marco,
|
| 73 |
+
title="Project Janus: Marco (Safe Mode)",
|
| 74 |
+
description="Marco (Versione Anti-Crash)"
|
| 75 |
)
|
| 76 |
|
| 77 |
if __name__ == "__main__":
|