Update app.py
Browse files
app.py
CHANGED
@@ -4,73 +4,25 @@ import requests
|
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
import asyncio
|
7 |
-
from smolagents import ToolCallingAgent, InferenceClientModel
|
8 |
from smolagents import DuckDuckGoSearchTool, Tool, CodeAgent
|
9 |
from huggingface_hub import login
|
10 |
-
#
|
11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
12 |
|
13 |
login(token=os.environ["HUGGINGFACEHUB_API_TOKEN"])
|
14 |
|
15 |
-
## TOOL 1
|
16 |
search_tool = DuckDuckGoSearchTool()
|
17 |
|
18 |
-
# --- Tool 1: Wikipedia Search Tool ---
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
# --- Tool 2: Audio Transcription Tool (uses whisper base) ---
|
23 |
-
import whisper
|
24 |
-
import tempfile
|
25 |
-
import base64
|
26 |
-
|
27 |
-
class AudioTranscriptionTool(Tool):
|
28 |
-
name = "audio_transcriber"
|
29 |
-
description = "Transcribes an uploaded audio file to text using Whisper."
|
30 |
-
|
31 |
-
inputs = {
|
32 |
-
"audio": {
|
33 |
-
"type": "string", # base64-encoded audio
|
34 |
-
"description": "Audio file (e.g. mp3, wav) encoded as base64 string"
|
35 |
-
}
|
36 |
-
}
|
37 |
-
|
38 |
-
output_type = "string"
|
39 |
-
|
40 |
-
def forward(self, audio: str) -> str:
|
41 |
-
try:
|
42 |
-
audio_bytes = base64.b64decode(audio)
|
43 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file:
|
44 |
-
tmp_file.write(audio_bytes)
|
45 |
-
tmp_path = tmp_file.name
|
46 |
-
|
47 |
-
model = whisper.load_model("base")
|
48 |
-
result = model.transcribe(tmp_path)
|
49 |
-
|
50 |
-
os.remove(tmp_path)
|
51 |
-
return result["text"]
|
52 |
-
|
53 |
-
except Exception as e:
|
54 |
-
return f"Transcription failed: {str(e)}"
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
#excel_tool = ExcelReaderTool()
|
62 |
-
#wiki_tool = WikipediaTool()
|
63 |
-
audio_tool = AudioTranscriptionTool()
|
64 |
-
|
65 |
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
66 |
log_output = ""
|
67 |
|
68 |
try:
|
69 |
-
agent =
|
70 |
-
tools=[search_tool
|
71 |
-
model=
|
72 |
-
max_steps=
|
73 |
-
verbosity_level=
|
74 |
)
|
75 |
except Exception as e:
|
76 |
yield f"Error initializing agent: {e}", None, log_output
|
@@ -83,12 +35,7 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
83 |
try:
|
84 |
response = requests.get(questions_url, timeout=15)
|
85 |
response.raise_for_status()
|
86 |
-
questions_data = response.json()
|
87 |
-
|
88 |
-
#selected_indices = [0,2,3,4,5,8,9,10,11,14,15,16,18] # 2nd, 5th, 6th, and 8th questions
|
89 |
-
#questions_data = [questions_data[i] for i in selected_indices if i < len(questions_data)]
|
90 |
-
|
91 |
-
|
92 |
if not questions_data:
|
93 |
yield "Fetched questions list is empty or invalid format.", None, log_output
|
94 |
return
|
@@ -116,17 +63,6 @@ async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
|
116 |
"FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. "
|
117 |
"If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. "
|
118 |
"If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. "
|
119 |
-
"If the answer is that no such event or person exists, respond with the single word 'none' or 'no one' as appropriate. Avoid long explanations."
|
120 |
-
"ALWAYS answer with the least amount of words (single word wherever possible). If the answer is 'right', say 'right, and not 'the answer is right'"
|
121 |
-
"Only respond with your final answer using the format: FINAL ANSWER: [your answer]. Do not include reasoning, context, explanations, markdown, or additional sections."
|
122 |
-
"Do not provide short answers and detailed answers seperately. You ONLY have to answer with the shortest answer possible. We DO NOT need explanations. Also dont use 'task outcome:'"
|
123 |
-
"You are a precise and concise AI assistant competing in a strict evaluation benchmark."
|
124 |
-
"You are a precise and concise AI assistant competing in a strict evaluation benchmark. You will be asked a question. You must only output a single final answer, using the following format exactly: FINAL ANSWER: [your answer]"
|
125 |
-
"Guidelines: Do NOT explain your reasoning. Do NOT output markdown, sections, titles, bullet points, or formatting like (Task outcome), (Detailed version), or (Additional context). Do NOT include references, sources, or citations. Do NOT include any leading text like (Here is the final answer), (The correct answer is), or (Answer):. Do NOT repeat the question or summarize it."
|
126 |
-
"Your response must consist of a single line starting with: FINAL ANSWER: and nothing else."
|
127 |
-
"Normalization rules: If the answer is a list, apply the above rules to each item, and separate them with commas. If the answer is a number, write it as digits (no commas, no units unless asked). If the answer is a string, use lowercase, no articles (a, an, the), and no abbreviations."
|
128 |
-
"If there is no correct answer (e.g., no such person, place, or event), respond with: FINAL ANSWER: none"
|
129 |
-
"When you call the wikipedia search tool, just answer in a single word. Do not explain your reasoning"
|
130 |
"If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.\n\n"
|
131 |
)
|
132 |
full_prompt = system_prompt + f"Question: {question_text.strip()}"
|
@@ -232,4 +168,4 @@ if __name__ == "__main__":
|
|
232 |
print(f"✅ SPACE_ID: https://huggingface.co/spaces/{space_id_startup}")
|
233 |
|
234 |
print("Launching Gradio Interface...")
|
235 |
-
demo.launch(debug=True, share=False)
|
|
|
4 |
import inspect
|
5 |
import pandas as pd
|
6 |
import asyncio
|
7 |
+
from smolagents import ToolCallingAgent, InferenceClientModel, HfApiModel
|
8 |
from smolagents import DuckDuckGoSearchTool, Tool, CodeAgent
|
9 |
from huggingface_hub import login
|
10 |
+
#h
|
11 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
12 |
|
13 |
login(token=os.environ["HUGGINGFACEHUB_API_TOKEN"])
|
14 |
|
|
|
15 |
search_tool = DuckDuckGoSearchTool()
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
async def run_and_submit_all(profile: gr.OAuthProfile | None):
|
18 |
log_output = ""
|
19 |
|
20 |
try:
|
21 |
+
agent = CodeAgent(
|
22 |
+
tools=[search_tool],
|
23 |
+
model=HfApiModel(model="MiniMaxAI/MiniMax-M1-80k"),
|
24 |
+
max_steps=5,
|
25 |
+
verbosity_level=2
|
26 |
)
|
27 |
except Exception as e:
|
28 |
yield f"Error initializing agent: {e}", None, log_output
|
|
|
35 |
try:
|
36 |
response = requests.get(questions_url, timeout=15)
|
37 |
response.raise_for_status()
|
38 |
+
questions_data = response.json()[:5]
|
|
|
|
|
|
|
|
|
|
|
39 |
if not questions_data:
|
40 |
yield "Fetched questions list is empty or invalid format.", None, log_output
|
41 |
return
|
|
|
63 |
"FINAL ANSWER: [YOUR FINAL ANSWER]. YOUR FINAL ANSWER should be a number OR as few words as possible OR a comma separated list of numbers and/or strings. "
|
64 |
"If you are asked for a number, don't use comma to write your number neither use units such as $ or percent sign unless specified otherwise. "
|
65 |
"If you are asked for a string, don't use articles, neither abbreviations (e.g. for cities), and write the digits in plain text unless specified otherwise. "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
"If you are asked for a comma separated list, apply the above rules depending of whether the element to be put in the list is a number or a string.\n\n"
|
67 |
)
|
68 |
full_prompt = system_prompt + f"Question: {question_text.strip()}"
|
|
|
168 |
print(f"✅ SPACE_ID: https://huggingface.co/spaces/{space_id_startup}")
|
169 |
|
170 |
print("Launching Gradio Interface...")
|
171 |
+
demo.launch(debug=True, share=False)
|