File size: 1,988 Bytes
6af7186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a9f87a
6af7186
adf056a
0dfacb5
 
 
 
adf056a
6af7186
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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"
        )

     loading_trigger = gr.Button("Trigger Loading Indicator")
     def show_loading_indicator():
         return "<div id='loading-indicator' style='display:block;'>Caricamento...</div>"
     loading_trigger.click(show_loading_indicator, outputs=loading_indicator)
    
    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)