Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,51 +4,44 @@ import os
|
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
import pandas as pd
|
7 |
-
from PIL import Image
|
8 |
-
import base64
|
9 |
-
import io
|
10 |
|
11 |
-
import
|
12 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, LiteLLMModel
|
13 |
|
14 |
-
# System prompt
|
15 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
|
16 |
-
Report your thoughts, and finish your answer with just the answer — no prefixes like
|
17 |
Your answer should be a number OR as few words as possible OR a comma-separated list of numbers and/or strings.
|
18 |
If you're asked for a number, don’t use commas or units like $ or %, unless specified.
|
19 |
If you're asked for a string, don’t use articles or abbreviations (e.g. for cities), and write digits in plain text unless told otherwise."""
|
20 |
|
21 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
22 |
|
23 |
-
#
|
24 |
-
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
25 |
-
|
26 |
-
# Agent wrapper
|
27 |
class MyAgent:
|
28 |
def __init__(self):
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
|
50 |
# Main evaluation function
|
51 |
-
def run_and_submit_all(profile
|
52 |
space_id = os.getenv("SPACE_ID")
|
53 |
|
54 |
if profile:
|
@@ -73,30 +66,6 @@ def run_and_submit_all(profile, uploaded_code: list[gr.File] | None, uploaded_ex
|
|
73 |
except Exception as e:
|
74 |
return f"Error fetching questions: {e}", None
|
75 |
|
76 |
-
uploaded_code_str = ""
|
77 |
-
if uploaded_code:
|
78 |
-
try:
|
79 |
-
uploaded_file = uploaded_code[0]
|
80 |
-
uploaded_code_str = uploaded_file.read().decode("utf-8")
|
81 |
-
except Exception as e:
|
82 |
-
uploaded_code_str = f"# Failed to load uploaded code: {e}"
|
83 |
-
|
84 |
-
uploaded_excel_df = None
|
85 |
-
if uploaded_excel:
|
86 |
-
try:
|
87 |
-
uploaded_excel_df = pd.read_excel(uploaded_excel[0].name)
|
88 |
-
except Exception as e:
|
89 |
-
print(f"Error reading Excel: {e}")
|
90 |
-
uploaded_excel_df = None
|
91 |
-
|
92 |
-
uploaded_image_obj = None
|
93 |
-
if uploaded_image:
|
94 |
-
try:
|
95 |
-
uploaded_image_obj = Image.open(uploaded_image[0].name)
|
96 |
-
except Exception as e:
|
97 |
-
print(f"Error loading image: {e}")
|
98 |
-
uploaded_image_obj = None
|
99 |
-
|
100 |
results_log = []
|
101 |
answers_payload = []
|
102 |
|
@@ -106,14 +75,9 @@ def run_and_submit_all(profile, uploaded_code: list[gr.File] | None, uploaded_ex
|
|
106 |
if not task_id or question_text is None:
|
107 |
continue
|
108 |
try:
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
uploaded_excel_df if "excel" in question_text.lower() or "spreadsheet" in question_text.lower() else None,
|
113 |
-
uploaded_image_obj if "image" in question_text.lower() or "photo" in question_text.lower() or "jpg" in question_text.lower() else None
|
114 |
-
)
|
115 |
-
answers_payload.append({"task_id": task_id, "submitted_answer": answer})
|
116 |
-
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": answer})
|
117 |
except Exception as e:
|
118 |
results_log.append({
|
119 |
"Task ID": task_id,
|
@@ -152,23 +116,15 @@ with gr.Blocks() as demo:
|
|
152 |
**Instructions:**
|
153 |
1. Clone this space and configure your Gemini API key.
|
154 |
2. Log in to Hugging Face.
|
155 |
-
3.
|
156 |
-
4. Run your agent on evaluation tasks and submit answers.
|
157 |
""")
|
158 |
|
159 |
gr.LoginButton()
|
160 |
-
code_upload = gr.File(label="Upload Python code file", file_types=[".py"])
|
161 |
-
excel_upload = gr.File(label="Upload Excel file", file_types=[".xls", ".xlsx"])
|
162 |
-
image_upload = gr.File(label="Upload Image file", file_types=[".jpg", ".jpeg"])
|
163 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
164 |
status_output = gr.Textbox(label="Submission Result", lines=5, interactive=False)
|
165 |
results_table = gr.DataFrame(label="Results", wrap=True)
|
166 |
|
167 |
-
run_button.click(
|
168 |
-
fn=run_and_submit_all,
|
169 |
-
inputs=[gr.State(), code_upload, excel_upload, image_upload],
|
170 |
-
outputs=[status_output, results_table]
|
171 |
-
)
|
172 |
|
173 |
if __name__ == "__main__":
|
174 |
print("🔧 App starting...")
|
@@ -176,3 +132,4 @@ if __name__ == "__main__":
|
|
176 |
|
177 |
|
178 |
|
|
|
|
4 |
import gradio as gr
|
5 |
import requests
|
6 |
import pandas as pd
|
|
|
|
|
|
|
7 |
|
8 |
+
from smolagents import LiteLLMModel, CodeAgent, DuckDuckGoSearchTool
|
|
|
9 |
|
10 |
+
# System prompt for the agent
|
11 |
SYSTEM_PROMPT = """You are a general AI assistant. I will ask you a question.
|
12 |
+
Report your thoughts, and finish your answer with just the answer — no prefixes like "FINAL ANSWER:".
|
13 |
Your answer should be a number OR as few words as possible OR a comma-separated list of numbers and/or strings.
|
14 |
If you're asked for a number, don’t use commas or units like $ or %, unless specified.
|
15 |
If you're asked for a string, don’t use articles or abbreviations (e.g. for cities), and write digits in plain text unless told otherwise."""
|
16 |
|
17 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
18 |
|
19 |
+
# Agent wrapper using LiteLLMModel
|
|
|
|
|
|
|
20 |
class MyAgent:
|
21 |
def __init__(self):
|
22 |
+
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
23 |
+
if not gemini_api_key:
|
24 |
+
raise ValueError("GEMINI_API_KEY not set in environment variables.")
|
25 |
+
|
26 |
+
# Instantiate LiteLLMModel with Gemini API key and model id
|
27 |
+
self.model = LiteLLMModel(
|
28 |
+
model_id="gemini/gemini-2.0-flash-lite",
|
29 |
+
api_key=gemini_api_key,
|
30 |
+
system_prompt=SYSTEM_PROMPT
|
31 |
+
)
|
32 |
+
|
33 |
+
# Create the CodeAgent with optional base tools and DuckDuckGo search
|
34 |
+
self.agent = CodeAgent(
|
35 |
+
tools=[DuckDuckGoSearchTool()],
|
36 |
+
model=self.model,
|
37 |
+
add_base_tools=True,
|
38 |
+
)
|
39 |
+
|
40 |
+
def __call__(self, question: str) -> str:
|
41 |
+
return self.agent.run(question)
|
42 |
|
43 |
# Main evaluation function
|
44 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
45 |
space_id = os.getenv("SPACE_ID")
|
46 |
|
47 |
if profile:
|
|
|
66 |
except Exception as e:
|
67 |
return f"Error fetching questions: {e}", None
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
results_log = []
|
70 |
answers_payload = []
|
71 |
|
|
|
75 |
if not task_id or question_text is None:
|
76 |
continue
|
77 |
try:
|
78 |
+
submitted_answer = agent(question_text)
|
79 |
+
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
80 |
+
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
|
|
|
|
|
|
|
81 |
except Exception as e:
|
82 |
results_log.append({
|
83 |
"Task ID": task_id,
|
|
|
116 |
**Instructions:**
|
117 |
1. Clone this space and configure your Gemini API key.
|
118 |
2. Log in to Hugging Face.
|
119 |
+
3. Run your agent on evaluation tasks and submit answers.
|
|
|
120 |
""")
|
121 |
|
122 |
gr.LoginButton()
|
|
|
|
|
|
|
123 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
124 |
status_output = gr.Textbox(label="Submission Result", lines=5, interactive=False)
|
125 |
results_table = gr.DataFrame(label="Results", wrap=True)
|
126 |
|
127 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
128 |
|
129 |
if __name__ == "__main__":
|
130 |
print("🔧 App starting...")
|
|
|
132 |
|
133 |
|
134 |
|
135 |
+
|