rubenml commited on
Commit
f78adb6
·
verified ·
1 Parent(s): 1ac0eba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -22
app.py CHANGED
@@ -3,37 +3,142 @@ import gradio as gr
3
  import requests
4
  import inspect
5
  import pandas as pd
6
- from transformers import pipeline
7
- import numpy as np
8
-
9
  # (Keep Constants as is)
10
  # --- Constants ---
11
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  # --- Basic Agent Definition ---
14
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
 
 
 
 
 
 
15
  class BasicAgent:
16
  def __init__(self):
17
- print("Initializing BERT-based QA agent...")
18
- # Load the BERT model fine-tuned on the SQuAD dataset
19
- self.qa_pipeline = pipeline("question-answering", model="bert-large-uncased-whole-word-masking-finetuned-squad")
20
-
21
- def __call__(self, question: str) -> str:
22
- """
23
- Process the question using the BERT model fine-tuned on SQuAD, and return an answer based on the context.
24
- """
25
- try:
26
- context = f"answer the following question as briefly as possible{question}"
27
- # Use the QA pipeline to get an answer based on the context and the question
28
- result = self.qa_pipeline(question=question, context=context)
29
- answer = result["answer"]
30
- except Exception as e:
31
- print(f"Error during QA: {e}")
32
- answer = "Error processing question."
 
 
 
 
 
 
 
 
 
 
33
 
34
- return answer
 
 
 
 
 
 
 
35
 
 
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  def run_and_submit_all( profile: gr.OAuthProfile | None):
38
  """
39
  Fetches all questions, runs the BasicAgent on them, submits all answers,
@@ -161,11 +266,9 @@ with gr.Blocks() as demo:
161
  gr.Markdown(
162
  """
163
  **Instructions:**
164
-
165
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
166
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
167
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
168
-
169
  ---
170
  **Disclaimers:**
171
  Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).
 
3
  import requests
4
  import inspect
5
  import pandas as pd
6
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, OpenAIServerModel, VisitWebpageTool, Tool, HfApiModel, ToolCallingAgent
7
+ import io
 
8
  # (Keep Constants as is)
9
  # --- Constants ---
10
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
11
 
12
+ class AttachmentDownloadTool(Tool):
13
+ name = "attachment-downloader"
14
+ description = "Downloads the file associated with the given task_id. If it does not exist, return None. input: task_id。output: attachment files or None"
15
+ inputs = {
16
+ "task_id": {
17
+ "type": "str",
18
+ "description": "task_id that needs to download attachment files."
19
+ }
20
+ }
21
+ output_type = io.BytesIO
22
+
23
+ def forward(self, task_id):
24
+ download_url = f"{api_url}/files/"
25
+
26
+ try:
27
+ response = requests.get(download_url + task_id, stream=True, timeout=15)
28
+ if response.status_code != 200:
29
+ return None
30
+ file_obj = io.BytesIO(response.content)
31
+ file_obj.seek(0)
32
+ return file_obj
33
+ except Exception as e:
34
+ return None
35
+
36
+ class ImageCaptionTool(Tool):
37
+ name = "image-captioner"
38
+ description = "Identify the content of the input image and describe it in natural language. Input: image. Output: description text."
39
+ inputs = {
40
+ "image": {
41
+ "type": "image",
42
+ "description": "Images that need to be identified and described"
43
+ }
44
+ }
45
+ output_type = str
46
+
47
+ def setup(self):
48
+ self.model = OpenAIServerModel(
49
+ model_id="Qwen/Qwen2.5-VL-32B-Instruct",
50
+ api_base="https://api.siliconflow.cn/v1/",
51
+ api_key=os.getenv('MODEL_TOKEN'),
52
+ )
53
+
54
+ def forward(self, image):
55
+ prompt = "Please describe the content of this picture in detail."
56
+ return self.model(prompt, images=[image])
57
+
58
+ class AudioToTextTool(Tool):
59
+ name = "audio-to-text"
60
+ description = "Convert the input audio content to text. Input: audio. Output: text."
61
+ inputs = {
62
+ "audio": {
63
+ "type": "audio",
64
+ "description": "The audio file that needs to be transcribed"
65
+ }
66
+ }
67
+ output_type = str
68
+
69
+ def setup(self):
70
+ # 使用 HuggingFace Hub 上的 Whisper 大模型
71
+ self.model = HfApiModel(model_id="openai/whisper-large-v3") # 或其他支持音频转写的模型
72
+
73
+ def forward(self, audio):
74
+ prompt = "Please transcribe this audio content into text."
75
+ return self.model(prompt, audios=[audio])
76
+
77
  # --- Basic Agent Definition ---
78
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
79
+ # class BasicAgent:
80
+ # def __init__(self):
81
+ # print("BasicAgent initialized.")
82
+ # def __call__(self, question: str) -> str:
83
+ # print(f"Agent received question (first 50 chars): {question[:50]}...")
84
+ # fixed_answer = "This is a default answer."
85
+ # print(f"Agent returning fixed answer: {fixed_answer}")
86
+ # return fixed_answer
87
  class BasicAgent:
88
  def __init__(self):
89
+
90
+ self.think_model = OpenAIServerModel(
91
+ model_id="THUDM/GLM-Z1-32B-0414",
92
+ api_base="https://api.siliconflow.cn/v1/",
93
+ api_key=os.getenv('MODEL_TOKEN'),
94
+ )
95
+ self.base_model = OpenAIServerModel(
96
+ model_id="THUDM/GLM-4-32B-0414",
97
+ api_base="https://api.siliconflow.cn/v1/",
98
+ api_key=os.getenv('MODEL_TOKEN'),
99
+ )
100
+ # self.vision_model = OpenAIServerModel(
101
+ # model_id="Qwen/Qwen2.5-VL-32B-Instruct",
102
+ # api_base="https://api.siliconflow.cn/v1/",
103
+ # api_key=os.getenv('MODEL_TOKEN'),
104
+ # )
105
+
106
+ self.tools = [AttachmentDownloadTool, ImageCaptionTool, AudioToTextTool]
107
+
108
+ web_agent = ToolCallingAgent(
109
+ tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
110
+ model=self.base_model,
111
+ max_steps=10,
112
+ name="web_search_agent",
113
+ description="Runs web searches for you.",
114
+ )
115
 
116
+ self.agent = CodeAgent(
117
+ tools=self.tools,
118
+ model=self.think_model,
119
+ managed_agents=[web_agent,],
120
+ additional_authorized_imports=["time", "numpy", "pandas"],
121
+ max_steps=20
122
+ )
123
+ print("BasicAgent initialized.")
124
 
125
+ def __call__(self, question: str, images=None) -> str:
126
 
127
+ print(f"Agent received question (first 50 chars): {question[:50]}...")
128
+
129
+ try:
130
+ if images is not None:
131
+ result = self.agent.run(question, images=images)
132
+ else:
133
+ result = self.agent.run(question)
134
+
135
+ print(f"Agent returning answer: {result}")
136
+ return result
137
+ except Exception as e:
138
+ print(f"Agent error: {e}")
139
+ return f"AGENT ERROR: {e}"
140
+
141
+
142
  def run_and_submit_all( profile: gr.OAuthProfile | None):
143
  """
144
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
266
  gr.Markdown(
267
  """
268
  **Instructions:**
 
269
  1. Please clone this space, then modify the code to define your agent's logic, the tools, the necessary packages, etc ...
270
  2. Log in to your Hugging Face account using the button below. This uses your HF username for submission.
271
  3. Click 'Run Evaluation & Submit All Answers' to fetch questions, run your agent, submit answers, and see the score.
 
272
  ---
273
  **Disclaimers:**
274
  Once clicking on the "submit button, it can take quite some time ( this is the time for the agent to go through all the questions).