Spaces:
Sleeping
Sleeping
Commit
·
c803551
1
Parent(s):
d8028fc
Phi-4
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ from langchain.tools import tool
|
|
7 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
8 |
from langchain.agents import initialize_agent, AgentType
|
9 |
from bs4 import BeautifulSoup
|
|
|
10 |
|
11 |
## # Load environment variables from .env file
|
12 |
# --- Constants ---
|
@@ -218,6 +219,50 @@ def web_scrape_tool(url: str) -> str:
|
|
218 |
except Exception as e:
|
219 |
return f"error: {e}"
|
220 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
##-- Tool Discovery ---
|
222 |
# Use @tool for each function.
|
223 |
# Use get_all_tools() to auto-discover all decorated tools.
|
@@ -235,7 +280,9 @@ tools_list = [
|
|
235 |
image_caption,
|
236 |
ocr_image,
|
237 |
classify_image,
|
238 |
-
web_scrape_tool
|
|
|
|
|
239 |
]
|
240 |
|
241 |
tool_descriptions = "\n".join(f"- {tool.name}: {tool.description}" for tool in tools_list)
|
@@ -284,7 +331,7 @@ Instructions:
|
|
284 |
# Generate the chat interface, including the tools
|
285 |
|
286 |
llm = HuggingFaceEndpoint(
|
287 |
-
repo_id="
|
288 |
# repo_id="Qwen/Qwen2.5-32B-Instruct",
|
289 |
huggingfacehub_api_token=HF_ACCESS_KEY,
|
290 |
# model_kwargs={'prompt': system_prompt}
|
|
|
7 |
from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
|
8 |
from langchain.agents import initialize_agent, AgentType
|
9 |
from bs4 import BeautifulSoup
|
10 |
+
import base64
|
11 |
|
12 |
## # Load environment variables from .env file
|
13 |
# --- Constants ---
|
|
|
219 |
except Exception as e:
|
220 |
return f"error: {e}"
|
221 |
|
222 |
+
# --- TOOL 13: Audio to Text Transcription Tool ---
|
223 |
+
@tool
|
224 |
+
def audio_to_text(audio_url: str) -> str:
|
225 |
+
"""
|
226 |
+
Transcribe speech from an audio file URL to text using Hugging Face's Whisper model.
|
227 |
+
Input: A direct link to an audio file (e.g., .mp3, .wav).
|
228 |
+
Output: The transcribed text.
|
229 |
+
"""
|
230 |
+
api_url = "https://api-inference.huggingface.co/models/openai/whisper-large-v3"
|
231 |
+
headers = {"Authorization": f"Bearer {HF_ACCESS_KEY}"}
|
232 |
+
try:
|
233 |
+
# Download the audio file
|
234 |
+
audio_resp = requests.get(audio_url, timeout=30)
|
235 |
+
audio_resp.raise_for_status()
|
236 |
+
audio_bytes = audio_resp.content
|
237 |
+
# Encode audio as base64 for API
|
238 |
+
audio_b64 = base64.b64encode(audio_bytes).decode("utf-8")
|
239 |
+
payload = {
|
240 |
+
"inputs": audio_b64,
|
241 |
+
"parameters": {"return_timestamps": False}
|
242 |
+
}
|
243 |
+
resp = requests.post(api_url, headers=headers, json=payload, timeout=60)
|
244 |
+
resp.raise_for_status()
|
245 |
+
data = resp.json()
|
246 |
+
return data.get("text", "no_answer")
|
247 |
+
except Exception as e:
|
248 |
+
return f"error: {e}"
|
249 |
+
|
250 |
+
# --- TOOL 14: Python Code Executor Tool ---
|
251 |
+
@tool
|
252 |
+
def python_executor(code: str) -> str:
|
253 |
+
"""
|
254 |
+
Safely execute simple Python code and return the result.
|
255 |
+
Only supports expressions and basic statements (no imports, file I/O, or system access).
|
256 |
+
"""
|
257 |
+
try:
|
258 |
+
# Restrict built-ins for safety
|
259 |
+
allowed_builtins = {"abs": abs, "min": min, "max": max, "sum": sum, "len": len, "range": range}
|
260 |
+
# Only allow expressions, not statements
|
261 |
+
result = eval(code, {"__builtins__": allowed_builtins}, {})
|
262 |
+
return str(result)
|
263 |
+
except Exception as e:
|
264 |
+
return f"error: {e}"
|
265 |
+
|
266 |
##-- Tool Discovery ---
|
267 |
# Use @tool for each function.
|
268 |
# Use get_all_tools() to auto-discover all decorated tools.
|
|
|
280 |
image_caption,
|
281 |
ocr_image,
|
282 |
classify_image,
|
283 |
+
web_scrape_tool,
|
284 |
+
audio_to_text,
|
285 |
+
python_executor
|
286 |
]
|
287 |
|
288 |
tool_descriptions = "\n".join(f"- {tool.name}: {tool.description}" for tool in tools_list)
|
|
|
331 |
# Generate the chat interface, including the tools
|
332 |
|
333 |
llm = HuggingFaceEndpoint(
|
334 |
+
repo_id="microsoft/Phi-4-multimodal-instruct",
|
335 |
# repo_id="Qwen/Qwen2.5-32B-Instruct",
|
336 |
huggingfacehub_api_token=HF_ACCESS_KEY,
|
337 |
# model_kwargs={'prompt': system_prompt}
|