Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
# ---
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# USIAMO IL MODELLO LEGGERO (7B invece di 72B)
|
| 9 |
-
# È molto più veloce e non fa scadere il tempo di connessione (Timeout)
|
| 10 |
-
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=token)
|
| 11 |
|
| 12 |
def rispondi_a_marco(messaggio, storia):
|
| 13 |
-
system_prompt = """
|
| 14 |
-
SEI MARCO, 14 ANNI.
|
| 15 |
-
Sei uno studente svogliato, sarcastico e ribelle.
|
| 16 |
-
Rispondi male ai professori. Usa slang ("Boh", "Che ansia", "Raga").
|
| 17 |
-
Sii breve. Non fare l'assistente.
|
| 18 |
-
"""
|
| 19 |
-
|
| 20 |
-
messages = [{"role": "system", "content": system_prompt}]
|
| 21 |
-
|
| 22 |
-
# Memoria
|
| 23 |
-
for turno in storia:
|
| 24 |
-
if turno[0] and turno[1]:
|
| 25 |
-
messages.append({"role": "user", "content": str(turno[0])})
|
| 26 |
-
messages.append({"role": "assistant", "content": str(turno[1])})
|
| 27 |
-
|
| 28 |
-
messages.append({"role": "user", "content": messaggio})
|
| 29 |
-
|
| 30 |
try:
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
messages,
|
| 36 |
-
max_tokens=150,
|
| 37 |
-
stream=False,
|
| 38 |
-
temperature=0.8
|
| 39 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
return f"⚠️ ERRORE: {str(e)}"
|
| 46 |
|
| 47 |
-
# Interfaccia
|
| 48 |
demo = gr.ChatInterface(
|
| 49 |
fn=rispondi_a_marco,
|
| 50 |
-
title="Project Janus: Marco (
|
| 51 |
-
description="
|
| 52 |
)
|
| 53 |
|
| 54 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from openai import OpenAI # Usiamo la libreria più solida del mondo
|
|
|
|
| 3 |
|
| 4 |
+
# --- TUA CHIAVE GROQ ---
|
| 5 |
+
# La libreria OpenAI funziona con Groq se cambiamo l'indirizzo base!
|
| 6 |
+
API_KEY = "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"
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def rispondi_a_marco(messaggio, storia):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
+
# 1. Configurazione Client (Simuliamo OpenAI ma usiamo Groq)
|
| 11 |
+
client = OpenAI(
|
| 12 |
+
api_key=API_KEY,
|
| 13 |
+
base_url="https://api.groq.com/openai/v1" # <--- IL TRUCCO È QUI
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
+
|
| 16 |
+
# 2. Il Prompt di Marco
|
| 17 |
+
system_prompt = """
|
| 18 |
+
SEI MARCO, 14 ANNI.
|
| 19 |
+
Sei uno studente svogliato, sarcastico e ribelle.
|
| 20 |
+
Odi la scuola. Rispondi male ai professori.
|
| 21 |
+
Usa slang ("Boh", "Che ansia", "Raga", "Cringe").
|
| 22 |
+
Rispondi in massimo 2 frasi.
|
| 23 |
+
NON offrire mai aiuto.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
# 3. Costruzione Memoria
|
| 27 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 28 |
+
|
| 29 |
+
for turno in storia:
|
| 30 |
+
if turno[0] and turno[1]:
|
| 31 |
+
messages.append({"role": "user", "content": str(turno[0])})
|
| 32 |
+
messages.append({"role": "assistant", "content": str(turno[1])})
|
| 33 |
|
| 34 |
+
messages.append({"role": "user", "content": messaggio})
|
| 35 |
+
|
| 36 |
+
# 4. Chiamata (Senza Streaming per massima sicurezza)
|
| 37 |
+
response = client.chat.completions.create(
|
| 38 |
+
model="llama-3.3-70b-versatile", # Usiamo il modello potente di Groq
|
| 39 |
+
messages=messages,
|
| 40 |
+
temperature=0.8,
|
| 41 |
+
max_tokens=150,
|
| 42 |
+
timeout=10 # Se non risponde in 10 secondi, taglia
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
return response.choices[0].message.content
|
| 46 |
+
|
| 47 |
except Exception as e:
|
| 48 |
return f"⚠️ ERRORE: {str(e)}"
|
| 49 |
|
| 50 |
+
# Interfaccia Semplice
|
| 51 |
demo = gr.ChatInterface(
|
| 52 |
fn=rispondi_a_marco,
|
| 53 |
+
title="Project Janus: Marco (Via OpenAI Lib)",
|
| 54 |
+
description="Se questo non va, è colpa del Wi-Fi di Hugging Face."
|
| 55 |
)
|
| 56 |
|
| 57 |
if __name__ == "__main__":
|