rohilrao commited on
Commit
e0b7dcf
·
verified ·
1 Parent(s): cd3eb5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -47
app.py CHANGED
@@ -16,58 +16,32 @@ class BasicAgent:
16
  print("BasicAgent initialized.")
17
 
18
  # Create model using Hugging Face Inference API
19
- # This will use the HF_TOKEN environment variable if available
20
- try:
21
- # Using a good open model available on HF Inference API
22
- self.model = LiteLLMModel(
23
- model_id="huggingface/meta-llama/Llama-3.2-3B-Instruct",
24
- api_base="https://api-inference.huggingface.co/models/",
25
- # HF_TOKEN will be automatically used if set in environment
26
- max_tokens=1024
27
- )
28
- print("Initialized Hugging Face Inference API model")
29
- except Exception as e:
30
- print(f"Error initializing HF model: {e}")
31
- # Fallback to a simpler approach if model fails
32
- self.model = None
33
 
34
- # Create the CodeAgent with tools if model is available
35
- if self.model:
36
- try:
37
- self.agent = CodeAgent(
38
- tools=[
39
- DuckDuckGoSearchTool(),
40
- VisitWebpageTool(),
41
- FinalAnswerTool()
42
- ],
43
- model=self.model,
44
- additional_authorized_imports=["wikipedia", "requests", "json", "re", "datetime", "os"]
45
- )
46
- print("CodeAgent with tools initialized successfully")
47
- except Exception as e:
48
- print(f"Error initializing CodeAgent: {e}")
49
- self.agent = None
50
- else:
51
- self.agent = None
52
 
53
  def __call__(self, question: str) -> str:
54
  print(f"Agent received question (first 50 chars): {question[:50]}...")
55
 
56
- if self.agent:
57
- try:
58
- # Use smolagents CodeAgent to process the question
59
- answer = self.agent.run(question)
60
- print(f"Agent returning answer: {answer[:100]}...")
61
- return answer
62
- except Exception as e:
63
- print(f"Error in agent processing: {e}")
64
- # Return a fallback answer in case of errors
65
- return f"Error processing question: {str(e)}"
66
- else:
67
- # Fallback if agent initialization failed
68
- fallback_answer = "Agent initialization failed. Please check model configuration."
69
- print(f"Agent returning fallback answer: {fallback_answer}")
70
- return fallback_answer
71
 
72
  def run_and_submit_all(profile: gr.OAuthProfile | None):
73
  """
 
16
  print("BasicAgent initialized.")
17
 
18
  # Create model using Hugging Face Inference API
19
+ self.model = LiteLLMModel(
20
+ model_id="huggingface/meta-llama/Llama-3.2-3B-Instruct",
21
+ api_base="https://api-inference.huggingface.co/models/",
22
+ max_tokens=1024
23
+ )
24
+ print("Initialized Hugging Face Inference API model")
 
 
 
 
 
 
 
 
25
 
26
+ # Create the CodeAgent with tools
27
+ self.agent = CodeAgent(
28
+ tools=[
29
+ DuckDuckGoSearchTool(),
30
+ VisitWebpageTool(),
31
+ FinalAnswerTool()
32
+ ],
33
+ model=self.model,
34
+ additional_authorized_imports=["wikipedia", "requests", "json", "re", "datetime", "os"]
35
+ )
36
+ print("CodeAgent with tools initialized successfully")
 
 
 
 
 
 
 
37
 
38
  def __call__(self, question: str) -> str:
39
  print(f"Agent received question (first 50 chars): {question[:50]}...")
40
 
41
+ # Use smolagents CodeAgent to process the question
42
+ answer = self.agent.run(question)
43
+ print(f"Agent returning answer: {answer[:100]}...")
44
+ return answer
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  def run_and_submit_all(profile: gr.OAuthProfile | None):
47
  """