Spaces:
Running
Running
Update app.py
Browse files
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("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
def __call__(self, question: str) -> str:
|
132 |
-
print(f"
|
133 |
-
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
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 =
|
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
|