thuyentruong commited on
Commit
03f3bb6
·
verified ·
1 Parent(s): 449bba2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -37
app.py CHANGED
@@ -1,3 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  os.system("pip install -q smolagents ddgs litellm markdownify requests")
3
  os.system("pip install -q llm_rs")
@@ -5,55 +24,48 @@ import gradio as gr
5
  import requests
6
  import inspect
7
  import pandas as pd
8
- from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel, InferenceClientModel, VisitWebpageTool
9
  import time
10
  import random
11
- import time
12
- import gradio as gr
13
- from llm_rs import AutoModel
14
  import logging
15
  import numpy as np
16
  from typing import List
17
- import requests
18
  import markdownify
19
  from smolagents import tool
20
 
 
21
  api_key = os.environ.get('Google_api_key')
22
  llm = LiteLLMModel(model_id="gemini/gemini-2.0-flash-lite", api_key=api_key)
23
  SYS_PROMPT = """You are an assistant for answering questions.
24
  If you don't know the answer, it's OK to make a guess."""
25
 
 
26
  # --- Basic Agent Definition ---
27
- class BasicAgent:
28
  def __init__(self):
29
- self.agent = CodeAgent(tools=[DuckDuckGoSearchTool(),VisitWebpageTool()
30
  ], model=llm)
31
  print("BasicAgent initialized.")
 
32
  def __call__(self, question: str) -> str:
33
  print(f"Agent received question (first 50 chars): {question[:50]}...")
34
  final_answer = self.agent.run(question)
35
  print(f"Agent returning final answer: {final_answer}")
36
  return final_answer
37
 
 
38
  def run_and_submit_all(question_text):
39
  """
40
  Fetches the question, runs the BasicAgent on it and return answer
41
  """
42
- # --- Determine HF Space Runtime URL and Repo URL ---
43
- space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
44
-
45
-
46
- # 1. Instantiate Agent ( modify this part to create your agent)
47
  try:
48
  agent = BasicAgent()
49
  except Exception as e:
50
  print(f"Error instantiating agent: {e}")
51
  return f"Error initializing agent: {e}", None
52
- # In the case of an app running as a hugging Face space, this link points toward your codebase ( usefull for others so please keep it public)
53
- agent_code = f"https://huggingface.co/spaces/{space_id}/tree/main"
54
- print(agent_code)
55
-
56
- # 2. Run your Agent
57
  try:
58
  agent_answer = agent(question_text)
59
  except Exception as e:
@@ -80,25 +92,6 @@ demo.launch()
80
 
81
 
82
  if __name__ == "__main__":
83
- print("\n" + "-"*30 + " App Starting " + "-"*30)
84
- # Check for SPACE_HOST and SPACE_ID at startup for information
85
- space_host_startup = os.getenv("SPACE_HOST")
86
- space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
87
-
88
- if space_host_startup:
89
- print(f"✅ SPACE_HOST found: {space_host_startup}")
90
- print(f" Runtime URL should be: https://{space_host_startup}.hf.space")
91
- else:
92
- print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
93
-
94
- if space_id_startup: # Print repo URLs if SPACE_ID is found
95
- print(f"✅ SPACE_ID found: {space_id_startup}")
96
- print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
97
- print(f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main")
98
- else:
99
- print("ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined.")
100
-
101
- print("-"*(60 + len(" App Starting ")) + "\n")
102
 
103
  print("Launching Gradio Interface for Basic Agent Evaluation...")
104
- demo.launch(debug=True, share=False)
 
1
+ """
2
+ This is an example template to build a chatbox app using agentic AI
3
+
4
+ Given a question, this agent will:
5
+ + Derive websearch query
6
+ + Retrieve relevant information
7
+ + Interprete the information and return the answer
8
+
9
+ To make the agent more capable, we can add other tools in the future, for example, CSV loading, calculations
10
+
11
+ Agentic framework used: Smolagents (HuggingFace)
12
+ LLM: gemini/gemini-2.0-flash-lite
13
+
14
+ @Contributor: nhatquynhthuyen.truong@cotiviti.com
15
+ @Org: Cotiviti AU
16
+ @Release Date: 28 Aug 2025
17
+ @Last Update: 28 Aug 2025
18
+ """
19
+
20
  import os
21
  os.system("pip install -q smolagents ddgs litellm markdownify requests")
22
  os.system("pip install -q llm_rs")
 
24
  import requests
25
  import inspect
26
  import pandas as pd
27
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel, VisitWebpageTool
28
  import time
29
  import random
 
 
 
30
  import logging
31
  import numpy as np
32
  from typing import List
 
33
  import markdownify
34
  from smolagents import tool
35
 
36
+
37
  api_key = os.environ.get('Google_api_key')
38
  llm = LiteLLMModel(model_id="gemini/gemini-2.0-flash-lite", api_key=api_key)
39
  SYS_PROMPT = """You are an assistant for answering questions.
40
  If you don't know the answer, it's OK to make a guess."""
41
 
42
+
43
  # --- Basic Agent Definition ---
44
+ class BasicAgent:
45
  def __init__(self):
46
+ self.agent = CodeAgent(tools=[DuckDuckGoSearchTool(), VisitWebpageTool()
47
  ], model=llm)
48
  print("BasicAgent initialized.")
49
+
50
  def __call__(self, question: str) -> str:
51
  print(f"Agent received question (first 50 chars): {question[:50]}...")
52
  final_answer = self.agent.run(question)
53
  print(f"Agent returning final answer: {final_answer}")
54
  return final_answer
55
 
56
+
57
  def run_and_submit_all(question_text):
58
  """
59
  Fetches the question, runs the BasicAgent on it and return answer
60
  """
61
+ # 1. Instantiate Agent
 
 
 
 
62
  try:
63
  agent = BasicAgent()
64
  except Exception as e:
65
  print(f"Error instantiating agent: {e}")
66
  return f"Error initializing agent: {e}", None
67
+
68
+ # 2. Run Agent
 
 
 
69
  try:
70
  agent_answer = agent(question_text)
71
  except Exception as e:
 
92
 
93
 
94
  if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
  print("Launching Gradio Interface for Basic Agent Evaluation...")
97
+ demo.launch(debug=True, share=False)