vladd19 commited on
Commit
2a751b1
·
verified ·
1 Parent(s): d91db07

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -23
app.py CHANGED
@@ -1,10 +1,13 @@
1
  import os
 
2
  import io
3
  import base64
4
  import time
 
5
  import requests
6
  import pandas as pd
7
  import gradio as gr
 
8
 
9
  from typing import TypedDict, Annotated
10
  from langgraph.graph import StateGraph
@@ -20,9 +23,11 @@ from groq import Groq
20
 
21
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
22
 
23
- GROQ_TEXT_MODEL = "llama-3.3-70b-versatile" # для текстовых запросов
24
- GROQ_VISION_MODEL = "meta-llama/llama-4-scout-17b-16e-instruct" # для картинок
25
- GROQ_AUDIO_MODEL = "whisper-large-v3-turbo" # для аудио
 
 
26
 
27
  def get_groq_client() -> Groq:
28
  key = os.getenv("GROQ_API_KEY")
@@ -30,15 +35,35 @@ def get_groq_client() -> Groq:
30
  raise ValueError("GROQ_API_KEY secret not set!")
31
  return Groq(api_key=key)
32
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  def _fetch_task_bytes(task_id: str) -> tuple[bytes, str]:
35
- """Returns (raw_bytes, content_type)."""
36
- url = f"{DEFAULT_API_URL}/files/{task_id}"
37
- resp = requests.get(url, timeout=20)
38
- resp.raise_for_status()
39
- content_type = resp.headers.get("Content-Type", "").lower()
40
- return resp.content, content_type
41
-
 
 
 
 
 
 
 
 
42
 
43
  def _is_image(ct: str, data: bytes) -> bool:
44
  image_types = ("image/", "png", "jpeg", "jpg", "gif", "webp")
@@ -147,24 +172,41 @@ def transcribe_audio(task_id: str) -> str:
147
  @tool
148
  def read_text_file(task_id: str) -> str:
149
  """
150
- Download a text-based file (txt, csv, json, py, xlsx summary, etc.)
151
- for a GAIA task and return its content (up to 4000 characters).
152
  """
153
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
154
  data, ct = _fetch_task_bytes(task_id)
155
- except Exception as e:
156
- return f"Could not fetch file for task {task_id}: {e}"
157
-
158
- if _is_image(ct, data) or _is_audio(ct, data):
159
- return (
160
- f"File for task {task_id} is binary ({ct}). "
161
- "Use analyze_image or transcribe_audio instead."
162
- )
163
-
164
- try:
165
  return data.decode("utf-8", errors="replace")[:4000]
 
166
  except Exception as e:
167
- return f"Text decode error: {e}"
168
 
169
 
170
  TOOLS = [web_search_tool, wikipedia_tool, analyze_image, transcribe_audio, read_text_file]
 
1
  import os
2
+ import mimetypes
3
  import io
4
  import base64
5
  import time
6
+
7
  import requests
8
  import pandas as pd
9
  import gradio as gr
10
+ import pypdf
11
 
12
  from typing import TypedDict, Annotated
13
  from langgraph.graph import StateGraph
 
23
 
24
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
25
 
26
+ GROQ_TEXT_MODEL = os.getenv("GROQ_TEXT_MODEL", "llama-3.3-70b-versatile")
27
+ GROQ_VISION_MODEL = os.getenv("GROQ_VISION_MODEL", "meta-llama/llama-4-scout-17b-16e-instruct")
28
+ GROQ_AUDIO_MODEL = os.getenv("GROQ_AUDIO_MODEL", "whisper-large-v3-turbo")
29
+
30
+ GAIA_DIR = "./data/gaia"
31
 
32
  def get_groq_client() -> Groq:
33
  key = os.getenv("GROQ_API_KEY")
 
35
  raise ValueError("GROQ_API_KEY secret not set!")
36
  return Groq(api_key=key)
37
 
38
+ def _get_task_file_map() -> dict[str, str]:
39
+ result = {}
40
+ validation_dir = os.path.join(GAIA_DIR, "2023", "validation")
41
+ for file_name in os.listdir(validation_dir):
42
+ if file_name.endswith(".parquet"):
43
+ continue
44
+ task_id = os.path.splitext(file_name)[0]
45
+ result[task_id] = os.path.join(validation_dir, file_name)
46
+ return result
47
+
48
+ def get_task_file(task_id: str) -> str | None:
49
+ return _get_task_file_map().get(task_id)
50
 
51
  def _fetch_task_bytes(task_id: str) -> tuple[bytes, str]:
52
+ local_path = get_task_file(task_id)
53
+ if not local_path:
54
+ raise FileNotFoundError(f"No local file mapped for task_id: {task_id}")
55
+
56
+ if not os.path.exists(local_path):
57
+ raise FileNotFoundError(f"File not found at path: {local_path}")
58
+
59
+ with open(local_path, "rb") as f:
60
+ data = f.read()
61
+
62
+ content_type, _ = mimetypes.guess_type(local_path)
63
+ if not content_type:
64
+ content_type = "application/octet-stream"
65
+
66
+ return data, content_type
67
 
68
  def _is_image(ct: str, data: bytes) -> bool:
69
  image_types = ("image/", "png", "jpeg", "jpg", "gif", "webp")
 
172
  @tool
173
  def read_text_file(task_id: str) -> str:
174
  """
175
+ Read a text-based, spreadsheet (Excel), or PDF file
176
+ for a GAIA task and return its content or summary (up to 4000 characters).
177
  """
178
  try:
179
+ local_path = get_task_file(task_id)
180
+ if not local_path:
181
+ return f"No file attached for task {task_id}."
182
+
183
+ if local_path.endswith((".xlsx", ".xls")):
184
+ df = pd.read_excel(local_path)
185
+ summary = f"Excel file columns: {list(df.columns)}\nShape: {df.shape}\n\n"
186
+ summary += df.head(15).to_string()
187
+ return summary[:4000]
188
+
189
+ elif local_path.endswith(".pdf"):
190
+ try:
191
+ reader = pypdf.PdfReader(local_path)
192
+ text = ""
193
+ for page in reader.pages[:5]:
194
+ text += page.extract_text() + "\n"
195
+ return text[:4000]
196
+ except ImportError:
197
+ return "PDF file found, but 'pypdf' library is not installed in the environment."
198
+
199
  data, ct = _fetch_task_bytes(task_id)
200
+ if _is_image(ct, data) or _is_audio(ct, data):
201
+ return (
202
+ f"File for task {task_id} is binary image/audio. "
203
+ "Use analyze_image or transcribe_audio instead."
204
+ )
205
+
 
 
 
 
206
  return data.decode("utf-8", errors="replace")[:4000]
207
+
208
  except Exception as e:
209
+ return f"Error reading file for task {task_id}: {e}"
210
 
211
 
212
  TOOLS = [web_search_tool, wikipedia_tool, analyze_image, transcribe_audio, read_text_file]