Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,64 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
|
| 5 |
# --- TUA CHIAVE API ---
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# Creiamo il client
|
| 9 |
-
client = Groq(api_key=api_key)
|
| 10 |
|
| 11 |
def rispondi_a_marco(messaggio, storia):
|
| 12 |
-
#
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
try:
|
| 16 |
-
#
|
| 17 |
-
|
| 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.
|
| 25 |
-
- Se il docente fa il "prof", tu sbuffa.
|
| 26 |
-
- Non dire MAI "Come posso aiutarti".
|
| 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 |
-
|
| 60 |
|
| 61 |
# --- INTERFACCIA ---
|
| 62 |
-
# ChatInterface gestisce lo streaming automaticamente
|
| 63 |
interfaccia = gr.ChatInterface(
|
| 64 |
fn=rispondi_a_marco,
|
| 65 |
-
title="🏫 Project Janus",
|
| 66 |
-
description="
|
| 67 |
-
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
# --- TUA CHIAVE API ---
|
| 6 |
+
API_KEY = "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"
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def rispondi_a_marco(messaggio, storia):
|
| 9 |
+
# URL diretto di Groq (senza librerie intermedie)
|
| 10 |
+
url = "https://api.groq.com/openai/v1/chat/completions"
|
| 11 |
+
|
| 12 |
+
# Headers di autorizzazione
|
| 13 |
+
headers = {
|
| 14 |
+
"Authorization": f"Bearer {API_KEY}",
|
| 15 |
+
"Content-Type": "application/json"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# 1. Prompt
|
| 19 |
+
system_prompt = "Sei Marco, 14 anni. Sei uno studente ribelle, svogliato e sarcastico. Rispondi in modo breve e infastidito."
|
| 20 |
+
|
| 21 |
+
# 2. Costruzione Memoria
|
| 22 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 23 |
+
|
| 24 |
+
# Aggiungiamo la storia (se esiste)
|
| 25 |
+
for turno in storia:
|
| 26 |
+
if turno[0] and turno[1]:
|
| 27 |
+
messages.append({"role": "user", "content": str(turno[0])})
|
| 28 |
+
messages.append({"role": "assistant", "content": str(turno[1])})
|
| 29 |
+
|
| 30 |
+
# Aggiungiamo il messaggio attuale
|
| 31 |
+
messages.append({"role": "user", "content": messaggio})
|
| 32 |
+
|
| 33 |
+
# 3. Preparazione Dati
|
| 34 |
+
payload = {
|
| 35 |
+
"model": "llama-3.3-70b-versatile",
|
| 36 |
+
"messages": messages,
|
| 37 |
+
"temperature": 0.8,
|
| 38 |
+
"max_tokens": 150
|
| 39 |
+
}
|
| 40 |
|
| 41 |
try:
|
| 42 |
+
# 4. CHIAMATA DIRETTA (Senza streaming per massima stabilità)
|
| 43 |
+
response = requests.post(url, headers=headers, json=payload, timeout=20)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
# Se la chiamata va a buon fine (Codice 200)
|
| 46 |
+
if response.status_code == 200:
|
| 47 |
+
data = response.json()
|
| 48 |
+
return data['choices'][0]['message']['content']
|
| 49 |
+
else:
|
| 50 |
+
# Se c'è un errore, ci dice ESATTAMENTE qual è
|
| 51 |
+
return f"⚠️ ERRORE DAL SERVER: {response.status_code} - {response.text}"
|
| 52 |
+
|
| 53 |
except Exception as e:
|
| 54 |
+
return f"⛔ ERRORE DI CONNESSIONE: {str(e)}"
|
| 55 |
|
| 56 |
# --- INTERFACCIA ---
|
|
|
|
| 57 |
interfaccia = gr.ChatInterface(
|
| 58 |
fn=rispondi_a_marco,
|
| 59 |
+
title="🏫 Project Janus: Marco (Versione HTTP)",
|
| 60 |
+
description="Se questo non funziona, è un problema di rete di HuggingFace.",
|
| 61 |
+
theme="soft"
|
| 62 |
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|