Spaces:
Runtime error
Runtime error
Update app.py
Browse filesCreate app file
app.py
CHANGED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio import ChatMessage
|
3 |
+
from main import run_agent
|
4 |
+
|
5 |
+
def chat_interface(history, user_input):
|
6 |
+
if not user_input.strip():
|
7 |
+
return history, "Per favore, inserisci una domanda o un messaggio."
|
8 |
+
|
9 |
+
# Aggiungi messaggio utente
|
10 |
+
history = history + [ChatMessage(role="user", content=user_input)]
|
11 |
+
|
12 |
+
try:
|
13 |
+
# Ottieni risposta dall'agent
|
14 |
+
response = run_agent(user_input)
|
15 |
+
|
16 |
+
# Controllo per risposte vuote
|
17 |
+
if not response.strip():
|
18 |
+
response = "Non ho capito la domanda, per favore riprova."
|
19 |
+
|
20 |
+
# Aggiungi messaggio assistente
|
21 |
+
history = history + [ChatMessage(role="assistant", content=response)]
|
22 |
+
|
23 |
+
except Exception as e:
|
24 |
+
history = history + [ChatMessage(role="assistant", content=f"Errore: {str(e)}")]
|
25 |
+
|
26 |
+
return history, ""
|
27 |
+
|
28 |
+
with gr.Blocks() as demo:
|
29 |
+
gr.Markdown("# Agent Conversazionale con smolagents")
|
30 |
+
chatbot = gr.Chatbot(type="messages")
|
31 |
+
user_input = gr.Textbox(placeholder="Scrivi la tua domanda qui...", lines=2)
|
32 |
+
submit_btn = gr.Button("Invia")
|
33 |
+
|
34 |
+
# Indicazione di caricamento
|
35 |
+
with gr.Column():
|
36 |
+
loading_indicator = gr.HTML("<div id='loading-indicator' style='display:none;'>Caricamento...</div>")
|
37 |
+
submit_btn.click(
|
38 |
+
lambda user_input: [chat_interface(chatbot, user_input), user_input],
|
39 |
+
inputs=[chatbot, user_input],
|
40 |
+
outputs=[chatbot, user_input],
|
41 |
+
fn_name="generate_response"
|
42 |
+
).then(
|
43 |
+
None,
|
44 |
+
inputs=None,
|
45 |
+
outputs=None,
|
46 |
+
_js="(e) => { document.getElementById('loading-indicator').style.display = e.detail[0]? 'block' : 'none'; }"
|
47 |
+
)
|
48 |
+
|
49 |
+
user_input.submit(chat_interface, inputs=[chatbot, user_input], outputs=[chatbot, user_input])
|
50 |
+
|
51 |
+
if __name__ == "__main__":
|
52 |
+
demo.launch(server_name="127.0.0.1", server_port=7860, debug=True)
|