GT5557 commited on
Commit
4c4c78c
·
verified ·
1 Parent(s): dc6cc7a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -56
app.py CHANGED
@@ -1,15 +1,10 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
2
- import datetime
3
- import requests
4
- import pytz
5
  import yaml
 
 
6
  from tools.final_answer import FinalAnswerTool
7
 
8
-
9
-
10
-
11
-
12
- #new tool
13
  @tool
14
  def get_daily_word() -> str:
15
  """A tool that returns a motivational word for English learners."""
@@ -17,65 +12,36 @@ def get_daily_word() -> str:
17
  words = ["Persistence", "Eloquence", "Clarity", "Resilience", "Impact"]
18
  return f"Today's word is: {random.choice(words)}"
19
 
20
-
21
-
22
  final_answer = FinalAnswerTool()
23
 
24
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
25
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
26
-
27
  model = HfApiModel(
28
- max_tokens=2096,
29
- temperature=0.5,
30
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
31
- custom_role_conversions=None,
32
  )
33
 
34
-
35
-
36
  with open("prompts.yaml", 'r') as stream:
37
  prompt_templates = yaml.safe_load(stream)
38
-
39
  agent = CodeAgent(
40
  model=model,
41
- tools=[final_answer,get_daily_word,DuckDuckGoSearchTool()], ## add your tools here (don't remove final answer)
42
  max_steps=6,
43
- verbosity_level=1,
44
- grammar=None,
45
- planning_interval=None,
46
- name=None,
47
- description=None,
48
  prompt_templates=prompt_templates
49
  )
50
 
 
 
 
 
 
51
 
52
- import gradio as gr
53
-
54
- def handle_query(query):
55
- # This calls your agent and returns the generator/answer
56
- yield agent.run(query)
57
-
58
- # Minimalist UI compatible with Gradio 4, 5, and 6
59
- with gr.Blocks() as demo:
60
- gr.Markdown("# Agentic AI - Final Project")
61
- chatbot = gr.Chatbot(label="Agent") # No 'type' argument = No crash
62
- msg = gr.Textbox(label="Enter your task:")
63
- clear = gr.Button("Clear")
64
-
65
- def user(user_message, history):
66
- return "", history + [[user_message, None]]
67
-
68
- def bot(history):
69
- # Access the last message sent by user
70
- user_message = history[-1][0]
71
- # Get response from your agent
72
- bot_message = agent.run(user_message)
73
- history[-1][1] = bot_message
74
- yield history
75
-
76
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
77
- bot, chatbot, chatbot
78
- )
79
- clear.click(lambda: None, None, chatbot, queue=False)
80
 
81
- demo.launch()
 
 
1
+ import os
 
 
 
2
  import yaml
3
+ import gradio as gr
4
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, tool
5
  from tools.final_answer import FinalAnswerTool
6
 
7
+ # 1. Define your custom tool
 
 
 
 
8
  @tool
9
  def get_daily_word() -> str:
10
  """A tool that returns a motivational word for English learners."""
 
12
  words = ["Persistence", "Eloquence", "Clarity", "Resilience", "Impact"]
13
  return f"Today's word is: {random.choice(words)}"
14
 
 
 
15
  final_answer = FinalAnswerTool()
16
 
17
+ # 2. Setup the Model
 
 
18
  model = HfApiModel(
19
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
20
+ token=os.getenv("HF_TOKEN")
 
 
21
  )
22
 
23
+ # 3. Load prompts and initialize Agent
 
24
  with open("prompts.yaml", 'r') as stream:
25
  prompt_templates = yaml.safe_load(stream)
26
+
27
  agent = CodeAgent(
28
  model=model,
29
+ tools=[final_answer, get_daily_word, DuckDuckGoSearchTool()],
30
  max_steps=6,
 
 
 
 
 
31
  prompt_templates=prompt_templates
32
  )
33
 
34
+ # 4. Custom UI Logic (Safe for Gradio 6.0)
35
+ def answer_task(message, history):
36
+ # This runs the agent and returns the string response
37
+ response = agent.run(message)
38
+ return response
39
 
40
+ demo = gr.ChatInterface(
41
+ fn=answer_task,
42
+ title="Hugging Face Agentic AI Project",
43
+ description="Ask me for a daily word or to search the web!"
44
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ if __name__ == "__main__":
47
+ demo.launch()