GraterCerts / app.py
eugpal4's picture
Update app.py
6af7186 verified
raw
history blame
1.93 kB
import gradio as gr
from gradio import ChatMessage
from main import run_agent
def chat_interface(history, user_input):
if not user_input.strip():
return history, "Per favore, inserisci una domanda o un messaggio."
# Aggiungi messaggio utente
history = history + [ChatMessage(role="user", content=user_input)]
try:
# Ottieni risposta dall'agent
response = run_agent(user_input)
# Controllo per risposte vuote
if not response.strip():
response = "Non ho capito la domanda, per favore riprova."
# Aggiungi messaggio assistente
history = history + [ChatMessage(role="assistant", content=response)]
except Exception as e:
history = history + [ChatMessage(role="assistant", content=f"Errore: {str(e)}")]
return history, ""
with gr.Blocks() as demo:
gr.Markdown("# Agent Conversazionale con smolagents")
chatbot = gr.Chatbot(type="messages")
user_input = gr.Textbox(placeholder="Scrivi la tua domanda qui...", lines=2)
submit_btn = gr.Button("Invia")
# Indicazione di caricamento
with gr.Column():
loading_indicator = gr.HTML("<div id='loading-indicator' style='display:none;'>Caricamento...</div>")
submit_btn.click(
lambda user_input: [chat_interface(chatbot, user_input), user_input],
inputs=[chatbot, user_input],
outputs=[chatbot, user_input],
fn_name="generate_response"
).then(
None,
inputs=None,
outputs=None,
_js="(e) => { document.getElementById('loading-indicator').style.display = e.detail[0]? 'block' : 'none'; }"
)
user_input.submit(chat_interface, inputs=[chatbot, user_input], outputs=[chatbot, user_input])
if __name__ == "__main__":
demo.launch(server_name="127.0.0.1", server_port=7860, debug=True)