Update app.py
Browse files
app.py
CHANGED
@@ -3,19 +3,22 @@ import gradio as gr
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
|
|
6 |
|
7 |
|
8 |
# --- Constants ---
|
9 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
10 |
|
11 |
-
# --- Enhanced Agent Definition ---
|
12 |
# --- Enhanced Agent Definition ---
|
13 |
class GAIAAgent:
|
14 |
def __init__(self):
|
15 |
-
|
16 |
-
self.
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
19 |
|
20 |
def format_prompt(self, question: str, file_content: str = None) -> str:
|
21 |
prompt = (
|
@@ -31,24 +34,14 @@ class GAIAAgent:
|
|
31 |
filepath = os.path.join("./", filename)
|
32 |
if filename.endswith(".txt") and os.path.exists(filepath):
|
33 |
with open(filepath, "r") as file:
|
34 |
-
return file.read()[:1000]
|
35 |
return ""
|
36 |
|
37 |
def __call__(self, question: str, file_name: str = None) -> str:
|
38 |
file_content = self.read_file(file_name) if file_name else None
|
39 |
prompt = self.format_prompt(question, file_content)
|
40 |
-
|
41 |
-
|
42 |
-
"Content-Type": "application/json",
|
43 |
-
}
|
44 |
-
response = requests.post(self.api_url, headers=headers, json={"inputs": prompt})
|
45 |
-
result = response.json()
|
46 |
-
|
47 |
-
if isinstance(result, list) and "generated_text" in result[0]:
|
48 |
-
return result[0]["generated_text"].split("Answer:")[-1].strip()
|
49 |
-
else:
|
50 |
-
print(f"Unexpected response: {result}")
|
51 |
-
return "AGENT ERROR: Model inference failed."
|
52 |
|
53 |
|
54 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
|
3 |
import requests
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
+
from smolagents import HfApiModel
|
7 |
|
8 |
|
9 |
# --- Constants ---
|
10 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
11 |
|
|
|
12 |
# --- Enhanced Agent Definition ---
|
13 |
class GAIAAgent:
|
14 |
def __init__(self):
|
15 |
+
print("GAIAAgent with HfApiModel initialized.")
|
16 |
+
self.model = HfApiModel(
|
17 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
18 |
+
max_tokens=2096,
|
19 |
+
temperature=0.5,
|
20 |
+
custom_role_conversions=None,
|
21 |
+
)
|
22 |
|
23 |
def format_prompt(self, question: str, file_content: str = None) -> str:
|
24 |
prompt = (
|
|
|
34 |
filepath = os.path.join("./", filename)
|
35 |
if filename.endswith(".txt") and os.path.exists(filepath):
|
36 |
with open(filepath, "r") as file:
|
37 |
+
return file.read()[:1000]
|
38 |
return ""
|
39 |
|
40 |
def __call__(self, question: str, file_name: str = None) -> str:
|
41 |
file_content = self.read_file(file_name) if file_name else None
|
42 |
prompt = self.format_prompt(question, file_content)
|
43 |
+
result = self.model(prompt)
|
44 |
+
return result.strip().split("Answer:")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
|
47 |
def run_and_submit_all(profile: gr.OAuthProfile | None):
|