Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,62 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
api_key="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",
|
| 7 |
-
base_url="https://api.groq.com/openai/v1"
|
| 8 |
-
)
|
| 9 |
|
| 10 |
def rispondi_a_marco(messaggio, storia):
|
| 11 |
-
# 1. Prompt di Sistema
|
| 12 |
-
system_prompt = "SEI MARCO, 14 ANNI. Sei svogliato, ribelle e usi slang. Odi la scuola. Rispondi in massimo 2 frasi."
|
| 13 |
-
|
| 14 |
-
# 2. Costruzione Memoria
|
| 15 |
-
messages = [{"role": "system", "content": system_prompt}]
|
| 16 |
-
|
| 17 |
-
for turno in storia:
|
| 18 |
-
if turno[0] and turno[1]:
|
| 19 |
-
messages.append({"role": "user", "content": str(turno[0])})
|
| 20 |
-
messages.append({"role": "assistant", "content": str(turno[1])})
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": messaggio})
|
| 23 |
-
|
| 24 |
try:
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
model="llama-3.1-8b-instant",
|
| 28 |
messages=messages,
|
| 29 |
temperature=0.8,
|
| 30 |
max_tokens=150,
|
| 31 |
-
stream=
|
|
|
|
| 32 |
)
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
-
|
| 37 |
|
| 38 |
-
# Interfaccia
|
| 39 |
-
demo = gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from openai import OpenAI
|
| 3 |
+
import os
|
| 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 |
+
if turno[0] and turno[1]:
|
| 30 |
+
messages.append({"role": "user", "content": str(turno[0])})
|
| 31 |
+
messages.append({"role": "assistant", "content": str(turno[1])})
|
| 32 |
+
|
| 33 |
+
messages.append({"role": "user", "content": messaggio})
|
| 34 |
+
|
| 35 |
+
# --- CHIAMATA CON TIMEOUT ESTESO (60 SECONDI) ---
|
| 36 |
+
stream = client.chat.completions.create(
|
| 37 |
model="llama-3.1-8b-instant",
|
| 38 |
messages=messages,
|
| 39 |
temperature=0.8,
|
| 40 |
max_tokens=150,
|
| 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 |
+
yield f"⚠️ ERRORE: {str(e)}"
|
| 53 |
|
| 54 |
+
# Interfaccia
|
| 55 |
+
demo = gr.ChatInterface(
|
| 56 |
+
fn=rispondi_a_marco,
|
| 57 |
+
title="Project Janus: Marco (Timeout 60s)",
|
| 58 |
+
description="Timeout esteso a 60 secondi per connessioni lente."
|
| 59 |
+
)
|
| 60 |
|
| 61 |
if __name__ == "__main__":
|
| 62 |
demo.launch()
|