Kkanittha commited on
Commit
f827084
·
verified ·
1 Parent(s): 3a17259

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -2
app.py CHANGED
@@ -3,7 +3,11 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
- from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
 
 
 
 
7
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
@@ -11,7 +15,51 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
  # --- Basic Agent Definition ---
13
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
14
  class BasicAgent:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  def __init__(self):
16
  print("BasicAgent initialized.")
17
  model = InferenceClientModel(
@@ -30,7 +78,7 @@ class BasicAgent:
30
  print(f"Agent received question (first 50 chars): {question[:50]}...")
31
  fixed_answer = self.agent.run(question)
32
  print(f"Agent returning fixed answer: {fixed_answer}")
33
- return fixed_answer
34
 
35
  def run_and_submit_all( profile: gr.OAuthProfile | None):
36
  """
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ #from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
7
+ from langchain_community.llms import HuggingFaceHub
8
+ from langchain.agents import initialize_agent, Tool
9
+ from langchain.utilities import DuckDuckGoSearchAPIWrapper
10
+ import os
11
 
12
  # (Keep Constants as is)
13
  # --- Constants ---
 
15
 
16
  # --- Basic Agent Definition ---
17
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
18
+
19
  class BasicAgent:
20
+ def __init__(self):
21
+ print("BasicAgent initialized.")
22
+
23
+ # Load Hugging Face model via LangChain
24
+ self.llm = HuggingFaceHub(
25
+ repo_id="google/flan-t5-base", # text2text model
26
+ huggingfacehub_api_token=os.getenv("HF_TOKEN"),
27
+ model_kwargs={"temperature": 0.2, "max_new_tokens": 256}
28
+ )
29
+
30
+ # Setup DuckDuckGo search tool
31
+ search = DuckDuckGoSearchAPIWrapper()
32
+ tools = [
33
+ Tool(
34
+ name="DuckDuckGo Search",
35
+ func=search.run,
36
+ description="Useful for answering questions about current events"
37
+ )
38
+ ]
39
+
40
+ # Initialize LangChain agent
41
+ self.agent = initialize_agent(
42
+ tools, self.llm, agent="zero-shot-react-description", verbose=True
43
+ )
44
+
45
+ def __call__(self, question: str) -> str:
46
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
47
+ fixed_answer = self.agent.run(question)
48
+ print(f"Agent returning fixed answer: {fixed_answer}")
49
+ return fixed_answer
50
+
51
+
52
+ '''def __call__(self, question: str) -> str:
53
+ print(f"Agent received: {question[:50]}...")
54
+ try:
55
+ answer = self.agent.run(question)
56
+ print(f"Agent returning: {answer}")
57
+ return answer
58
+ except Exception as e:
59
+ return f"AGENT ERROR: {e}"'''
60
+
61
+
62
+ '''class BasicAgent:
63
  def __init__(self):
64
  print("BasicAgent initialized.")
65
  model = InferenceClientModel(
 
78
  print(f"Agent received question (first 50 chars): {question[:50]}...")
79
  fixed_answer = self.agent.run(question)
80
  print(f"Agent returning fixed answer: {fixed_answer}")
81
+ return fixed_answer'''
82
 
83
  def run_and_submit_all( profile: gr.OAuthProfile | None):
84
  """