DragonProgrammer commited on
Commit
5d01286
·
verified ·
1 Parent(s): bf57de2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -7
app.py CHANGED
@@ -3,7 +3,6 @@ import gradio as gr
3
  import requests
4
  import pandas as pd
5
  import datetime
6
- import requests
7
  import pytz
8
  from transformers import HfAgent
9
  from bs4 import BeautifulSoup
@@ -125,14 +124,31 @@ def safe_calculator(expression: str) -> str:
125
 
126
  # --- Basic Agent Definition ---
127
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
128
  class HfAgentWrapper:
129
  def __init__(self):
130
- print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
131
  def __call__(self, question: str) -> str:
132
- print(f"Agent received question (first 50 chars): {question[:50]}...")
133
- fixed_answer = "This is a default answer."
134
- print(f"Agent returning fixed answer: {fixed_answer}")
135
- return fixed_answer
 
 
 
 
136
 
137
  def run_and_submit_all( profile: gr.OAuthProfile | None):
138
  """
@@ -155,7 +171,7 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
155
 
156
  # 1. Instantiate Agent ( modify this part to create your agent)
157
  try:
158
- agent = BasicAgent()
159
  except Exception as e:
160
  print(f"Error instantiating agent: {e}")
161
  return f"Error initializing agent: {e}", None
 
3
  import requests
4
  import pandas as pd
5
  import datetime
 
6
  import pytz
7
  from transformers import HfAgent
8
  from bs4 import BeautifulSoup
 
124
 
125
  # --- Basic Agent Definition ---
126
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
127
+ # --- Agent Definition using HfAgent ---
128
  class HfAgentWrapper:
129
  def __init__(self):
130
+ print("Initializing HfAgentWrapper...")
131
+ model_repo = "google/gemma-1.1-7b-it" # Consider Mixtral for more power if resources allow
132
+
133
+ print(f"Loading HfAgent with model: {model_repo}")
134
+ try:
135
+ self.tools_list = [get_current_time_in_timezone, web_search, safe_calculator]
136
+ self.agent = HfAgent(model_repo, additional_tools=self.tools_list)
137
+ print(f"HfAgent loaded successfully with model {model_repo}.")
138
+ print(f"HfAgent initialized with tools: {[tool.__name__ for tool in self.tools_list]}")
139
+ except Exception as e:
140
+ print(f"CRITICAL ERROR: Failed to initialize HfAgent: {e}")
141
+ raise RuntimeError(f"HfAgent initialization failed: {e}") from e
142
+
143
  def __call__(self, question: str) -> str:
144
+ print(f"\n--- HfAgentWrapper received question (first 100 chars): {question[:100]}... ---")
145
+ try:
146
+ answer = self.agent.run(question)
147
+ print(f"--- HfAgentWrapper generated answer (first 100 chars): {str(answer)[:100]}... ---")
148
+ return str(answer)
149
+ except Exception as e:
150
+ print(f"ERROR: HfAgent execution failed for question '{question[:50]}...': {e}")
151
+ return f"Agent Error: Failed to process the question. Details: {e}"
152
 
153
  def run_and_submit_all( profile: gr.OAuthProfile | None):
154
  """
 
171
 
172
  # 1. Instantiate Agent ( modify this part to create your agent)
173
  try:
174
+ agent = HfAgentWrapper()
175
  except Exception as e:
176
  print(f"Error instantiating agent: {e}")
177
  return f"Error initializing agent: {e}", None