Thanh Vinh Vo commited on
Commit
37fcc0e
·
1 Parent(s): 072fb76
Files changed (1) hide show
  1. app.py +54 -2
app.py CHANGED
@@ -76,6 +76,58 @@ def get_image(file_name: str) -> str:
76
  raise ValueError(f"Failed to download image from {url}: {e}")
77
 
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  # --- Basic Agent Definition ---
80
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
81
  class BasicAgent:
@@ -105,7 +157,7 @@ class BasicAgent:
105
  )
106
 
107
  self.code_agent = CodeAgent(
108
- tools=[VisitWebpageTool(), DuckDuckGoSearchTool(), get_image],
109
  model=InferenceClientModel(
110
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
111
  ),
@@ -147,7 +199,7 @@ class BasicAgent:
147
  model=InferenceClientModel(
148
  "Qwen/Qwen2.5-32B-Instruct"
149
  ),
150
- tools=[get_image],
151
  managed_agents=[
152
  self.multimodal_agent,
153
  self.code_agent],
 
76
  raise ValueError(f"Failed to download image from {url}: {e}")
77
 
78
 
79
+ @tool
80
+ def get_text_file(file_name: str) -> str:
81
+ """
82
+ A tool that downloads a text file (such as code) from file name and saves it locally.
83
+ Args:
84
+ file_name: File name.
85
+ Returns:
86
+ str: Local file path where the text was saved.
87
+ """
88
+ import requests
89
+ import os
90
+
91
+ url = f"{DEFAULT_API_URL}/files/{file_name}"
92
+ print(f"Fetching text file from URL: {url}")
93
+
94
+ # Create downloads directory if it doesn't exist
95
+ downloads_dir = "downloaded_texts"
96
+ os.makedirs(downloads_dir, exist_ok=True)
97
+
98
+ response = None
99
+ try:
100
+ response = requests.get(url, timeout=30)
101
+ response.raise_for_status() # Raises an HTTPError for bad responses
102
+
103
+ # Check if response is empty
104
+ if not response.content:
105
+ raise ValueError(f"Empty response received from {url}")
106
+
107
+ # Check content type
108
+ content_type = response.headers.get('content-type', '').lower()
109
+ print(f"Response content-type: {content_type}")
110
+ print(f"Response content length: {len(response.content)} bytes")
111
+
112
+ # Use original filename directly
113
+ local_path = os.path.join(downloads_dir, file_name)
114
+
115
+ # Save the text file locally
116
+ with open(local_path, 'wb') as f:
117
+ f.write(response.content)
118
+
119
+ print(f"Text file saved to: {local_path}")
120
+ return local_path
121
+
122
+ except requests.exceptions.RequestException as e:
123
+ raise ValueError(f"Failed to fetch text file from {url}: {e}")
124
+ except Exception as e:
125
+ # Print first 200 characters of response content for debugging
126
+ content_preview = response.content[:200] if response and hasattr(response, 'content') else b"No response"
127
+ print(f"Error downloading text file. Content preview: {content_preview}")
128
+ raise ValueError(f"Failed to download text file from {url}: {e}")
129
+
130
+
131
  # --- Basic Agent Definition ---
132
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
133
  class BasicAgent:
 
157
  )
158
 
159
  self.code_agent = CodeAgent(
160
+ tools=[VisitWebpageTool(), DuckDuckGoSearchTool(), get_image, get_text_file],
161
  model=InferenceClientModel(
162
  model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
163
  ),
 
199
  model=InferenceClientModel(
200
  "Qwen/Qwen2.5-32B-Instruct"
201
  ),
202
+ tools=[get_image, get_text_file, VisitWebpageTool(), DuckDuckGoSearchTool()],
203
  managed_agents=[
204
  self.multimodal_agent,
205
  self.code_agent],