STARX33 commited on
Commit
0e1fcc9
·
verified ·
1 Parent(s): 0c05b5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -14
app.py CHANGED
@@ -6,26 +6,53 @@ from datetime import datetime
6
  from typing import Optional
7
  from smolagents import CodeAgent, HfApiModel, tool
8
  from duckduckgo_search import DDGS
 
9
  from PIL import Image
10
- import markdownify
11
 
12
  # === Tool 1: Current Time Tool ===
13
  @tool
14
  def get_current_time_in_timezone(timezone: Optional[str] = None) -> str:
 
 
 
 
 
 
 
 
 
15
  now = datetime.utcnow()
16
  if timezone:
17
  return f"The current time in {timezone} is approximately {now.strftime('%Y-%m-%d %H:%M:%S')} UTC."
18
- return f"The current UTC time is {now.strftime('%Y-%m-%d %H:%M:%S')}"
19
 
20
  # === Tool 2: CSV Parsing Tool ===
21
  @tool
22
  def parse_csv(file_path: str) -> str:
 
 
 
 
 
 
 
 
 
23
  df = pd.read_csv(file_path)
24
  return df.head(5).to_string(index=False)
25
 
26
  # === Tool 3: Audio Transcription Tool ===
27
  @tool
28
  def transcribe_audio(audio_path: str) -> str:
 
 
 
 
 
 
 
 
 
29
  import whisper
30
  model = whisper.load_model("base")
31
  result = model.transcribe(audio_path)
@@ -34,38 +61,57 @@ def transcribe_audio(audio_path: str) -> str:
34
  # === Tool 4: Web Search Tool ===
35
  @tool
36
  def duckduckgo_search(query: str) -> str:
 
 
 
 
 
 
 
 
 
37
  results = []
38
  with DDGS() as ddgs:
39
- for r in ddgs.text(query):
40
- results.append(f"{r['title']} - {r['href']}")
41
- if len(results) >= 3:
42
- break
43
- return "\n".join(results) if results else "No results found."
44
 
45
  # === Tool 5: Image Captioning Tool ===
46
  @tool
47
  def describe_image_with_blip(image_path: str) -> str:
48
- from transformers import BlipProcessor, BlipForConditionalGeneration
49
- import torch
50
 
 
 
 
 
 
 
51
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
52
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
53
-
54
- raw_image = Image.open(image_path).convert('RGB')
55
- inputs = processor(raw_image, return_tensors="pt")
56
  out = model.generate(**inputs)
57
  caption = processor.decode(out[0], skip_special_tokens=True)
58
  return caption
59
 
 
60
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
61
 
 
62
  class BasicAgent:
63
  def __init__(self):
64
  model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
65
  self.agent = CodeAgent(
66
  model=model,
67
- tools=[get_current_time_in_timezone, parse_csv, transcribe_audio, duckduckgo_search, describe_image_with_blip],
68
- add_base_tools=False,
 
 
 
 
 
69
  max_steps=6,
70
  name="EvalAgent",
71
  description="Certification agent using vision, search, time, audio, and csv tools"
@@ -79,6 +125,7 @@ class BasicAgent:
79
  response += f"\nFinal Answer: {last_line}"
80
  return response
81
 
 
82
  def run_and_submit_all(profile: gr.OAuthProfile | None):
83
  space_id = os.getenv("SPACE_ID")
84
 
@@ -155,6 +202,7 @@ def run_and_submit_all(profile: gr.OAuthProfile | None):
155
  except Exception as e:
156
  return f"Submission Failed: {e}", pd.DataFrame(results_log)
157
 
 
158
  with gr.Blocks() as demo:
159
  gr.Markdown("# Basic Agent Evaluation Runner")
160
  gr.Markdown("""
 
6
  from typing import Optional
7
  from smolagents import CodeAgent, HfApiModel, tool
8
  from duckduckgo_search import DDGS
9
+ from transformers import BlipProcessor, BlipForConditionalGeneration
10
  from PIL import Image
 
11
 
12
  # === Tool 1: Current Time Tool ===
13
  @tool
14
  def get_current_time_in_timezone(timezone: Optional[str] = None) -> str:
15
+ """
16
+ Returns the current time. If a timezone is provided, it is noted in the message.
17
+
18
+ Args:
19
+ timezone: An optional timezone name or location.
20
+
21
+ Returns:
22
+ A string with the current UTC time or specified timezone info.
23
+ """
24
  now = datetime.utcnow()
25
  if timezone:
26
  return f"The current time in {timezone} is approximately {now.strftime('%Y-%m-%d %H:%M:%S')} UTC."
27
+ return f"The current UTC time is {now.strftime('%Y-%m-%d %H:%M:%S')}."
28
 
29
  # === Tool 2: CSV Parsing Tool ===
30
  @tool
31
  def parse_csv(file_path: str) -> str:
32
+ """
33
+ Parses a CSV file and returns the first few rows.
34
+
35
+ Args:
36
+ file_path: Path to the CSV file to be parsed.
37
+
38
+ Returns:
39
+ A preview of the first rows as a string.
40
+ """
41
  df = pd.read_csv(file_path)
42
  return df.head(5).to_string(index=False)
43
 
44
  # === Tool 3: Audio Transcription Tool ===
45
  @tool
46
  def transcribe_audio(audio_path: str) -> str:
47
+ """
48
+ Transcribes spoken words in an audio file.
49
+
50
+ Args:
51
+ audio_path: Path to the .mp3 or .wav file.
52
+
53
+ Returns:
54
+ A transcription of the audio content as a string.
55
+ """
56
  import whisper
57
  model = whisper.load_model("base")
58
  result = model.transcribe(audio_path)
 
61
  # === Tool 4: Web Search Tool ===
62
  @tool
63
  def duckduckgo_search(query: str) -> str:
64
+ """
65
+ Performs a DuckDuckGo web search and returns the top 3 results.
66
+
67
+ Args:
68
+ query: The search query string.
69
+
70
+ Returns:
71
+ A formatted string with the top 3 search result titles and URLs.
72
+ """
73
  results = []
74
  with DDGS() as ddgs:
75
+ for r in ddgs.text(query, region='wt-wt', safesearch='Off', max_results=3):
76
+ results.append(f"- {r['title']}\n{r['href']}")
77
+ return "\n\n".join(results) if results else "No results found."
 
 
78
 
79
  # === Tool 5: Image Captioning Tool ===
80
  @tool
81
  def describe_image_with_blip(image_path: str) -> str:
82
+ """
83
+ Generates a caption describing the contents of an image.
84
 
85
+ Args:
86
+ image_path: Path to the image file to be described.
87
+
88
+ Returns:
89
+ A string caption generated by the BLIP vision-language model.
90
+ """
91
  processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
92
  model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
93
+ image = Image.open(image_path).convert("RGB")
94
+ inputs = processor(image, return_tensors="pt")
 
95
  out = model.generate(**inputs)
96
  caption = processor.decode(out[0], skip_special_tokens=True)
97
  return caption
98
 
99
+ # === Constants ===
100
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
101
 
102
+ # === Agent Definition ===
103
  class BasicAgent:
104
  def __init__(self):
105
  model = HfApiModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct")
106
  self.agent = CodeAgent(
107
  model=model,
108
+ tools=[
109
+ get_current_time_in_timezone,
110
+ parse_csv,
111
+ transcribe_audio,
112
+ duckduckgo_search,
113
+ describe_image_with_blip
114
+ ],
115
  max_steps=6,
116
  name="EvalAgent",
117
  description="Certification agent using vision, search, time, audio, and csv tools"
 
125
  response += f"\nFinal Answer: {last_line}"
126
  return response
127
 
128
+ # === Evaluation Logic ===
129
  def run_and_submit_all(profile: gr.OAuthProfile | None):
130
  space_id = os.getenv("SPACE_ID")
131
 
 
202
  except Exception as e:
203
  return f"Submission Failed: {e}", pd.DataFrame(results_log)
204
 
205
+ # === Gradio UI ===
206
  with gr.Blocks() as demo:
207
  gr.Markdown("# Basic Agent Evaluation Runner")
208
  gr.Markdown("""