Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
| 3 |
|
| 4 |
# --- TUA CHIAVE ---
|
| 5 |
-
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
messages = [{"role": "system", "content": sistema}]
|
| 20 |
-
# Aggiungiamo la storia recente
|
| 21 |
for turno in storia:
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
messages.append({"role": "user", "content": messaggio})
|
| 25 |
|
| 26 |
-
# Payload dati
|
| 27 |
-
payload = {
|
| 28 |
-
"model": "llama-3.3-70b-versatile",
|
| 29 |
-
"messages": messages,
|
| 30 |
-
"max_tokens": 100
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
try:
|
| 34 |
-
# Chiamata
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
except Exception as e:
|
| 43 |
-
|
| 44 |
|
| 45 |
-
# Interfaccia
|
| 46 |
-
demo = gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
# --- TUA CHIAVE ---
|
| 6 |
+
api_key = "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"
|
| 7 |
|
| 8 |
+
def rispondi_a_marco(messaggio, storia):
|
| 9 |
+
# 1. Creiamo il client QUI DENTRO (Fondamentale per il 2° messaggio)
|
| 10 |
+
# Se lo mettiamo fuori, la connessione cade dopo il primo turno!
|
| 11 |
+
client = Groq(api_key=api_key)
|
| 12 |
+
|
| 13 |
+
# 2. Prompt
|
| 14 |
+
system_prompt = """
|
| 15 |
+
SEI MARCO, 14 ANNI.
|
| 16 |
+
Sei ribelle, svogliato e usi slang.
|
| 17 |
+
Odi la scuola. Se l'utente fa il prof, lamentati.
|
| 18 |
+
"""
|
| 19 |
|
| 20 |
+
# 3. Costruiamo la memoria pulita
|
| 21 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 22 |
|
| 23 |
+
# Aggiungiamo la storia vecchia
|
|
|
|
|
|
|
| 24 |
for turno in storia:
|
| 25 |
+
if turno[0] and turno[1]: # Controllo anti-crash
|
| 26 |
+
messages.append({"role": "user", "content": str(turno[0])})
|
| 27 |
+
messages.append({"role": "assistant", "content": str(turno[1])})
|
| 28 |
+
|
| 29 |
+
# Aggiungiamo il messaggio nuovo
|
| 30 |
messages.append({"role": "user", "content": messaggio})
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
try:
|
| 33 |
+
# 4. Chiamata con STREAMING (Per evitare timeout)
|
| 34 |
+
stream = client.chat.completions.create(
|
| 35 |
+
model="llama-3.3-70b-versatile",
|
| 36 |
+
messages=messages,
|
| 37 |
+
temperature=0.8,
|
| 38 |
+
max_tokens=150,
|
| 39 |
+
stream=True
|
| 40 |
+
)
|
| 41 |
|
| 42 |
+
# Generiamo la risposta pezzo per pezzo
|
| 43 |
+
parola_parziale = ""
|
| 44 |
+
for chunk in stream:
|
| 45 |
+
token = chunk.choices[0].delta.content
|
| 46 |
+
if token:
|
| 47 |
+
parola_parziale += token
|
| 48 |
+
yield parola_parziale
|
| 49 |
+
|
| 50 |
except Exception as e:
|
| 51 |
+
yield f"⚠️ ERRORE: {str(e)}"
|
| 52 |
|
| 53 |
+
# Interfaccia
|
| 54 |
+
demo = gr.ChatInterface(
|
| 55 |
+
fn=rispondi_a_marco,
|
| 56 |
+
title="Project Janus: Marco",
|
| 57 |
+
description="Ora la connessione si rinnova a ogni messaggio."
|
| 58 |
+
)
|
| 59 |
|
| 60 |
+
if __name__ == "__main__":
|
| 61 |
+
demo.launch()
|