File size: 1,433 Bytes
10e9b7d
 
827c4db
 
 
10e9b7d
827c4db
 
 
e80aab9
827c4db
 
 
 
4021bf3
827c4db
 
3c4371f
827c4db
 
3c4371f
827c4db
eccf8e4
827c4db
 
 
 
7d65c66
827c4db
 
e80aab9
827c4db
e80aab9
827c4db
 
 
 
 
 
 
 
 
 
 
 
e80aab9
827c4db
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
import os
import gradio as gr
from agent import build_agent_executor
from huggingface_hub import InferenceClient
import logging

# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Load Hugging Face token
HF_TOKEN = os.getenv("HF_TOKEN")
if not HF_TOKEN:
    raise EnvironmentError("HF_TOKEN is missing. Please set it as an environment variable.")

# Initialize LLM client
llm_client = InferenceClient(token=HF_TOKEN)

# Build the agent
agent_executor = build_agent_executor(llm_client)

def run_agent(user_input: str) -> str:
    try:
        logger.info("User input: %s", user_input)
        response = agent_executor.run(user_input)
        logger.info("Agent response: %s", response)
        return response
    except Exception as e:
        logger.error("Error during agent execution: %s", e, exc_info=True)
        return f"Something went wrong: {e}"

# Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("## Professional HF Agent\nEnter a query and watch the agent in action.")
    
    with gr.Row():
        user_input = gr.Textbox(label="Your Question", placeholder="Ask me anything...", lines=2)
    
    with gr.Row():
        submit_btn = gr.Button("Submit")
        output_box = gr.Textbox(label="Agent Response", lines=6)

    submit_btn.click(fn=run_agent, inputs=user_input, outputs=output_box)

# Run the Gradio app
if __name__ == "__main__":
    demo.launch()