Spaces:
Runtime error
Runtime error
Upload 9 files
Browse files- agent/__pycache__/agent.cpython-312.pyc +0 -0
- agent/__pycache__/handlers.cpython-312.pyc +0 -0
- agent/__pycache__/tools.cpython-312.pyc +0 -0
- agent/__pycache__/utils.cpython-312.pyc +0 -0
- agent/agent.py +87 -0
- agent/handlers.py +273 -0
- agent/tempCodeRunnerFile.py +1 -0
- agent/tools.py +115 -0
- agent/utils.py +99 -0
agent/__pycache__/agent.cpython-312.pyc
ADDED
|
Binary file (1.54 kB). View file
|
|
|
agent/__pycache__/handlers.cpython-312.pyc
ADDED
|
Binary file (13.2 kB). View file
|
|
|
agent/__pycache__/tools.cpython-312.pyc
ADDED
|
Binary file (2.52 kB). View file
|
|
|
agent/__pycache__/utils.cpython-312.pyc
ADDED
|
Binary file (2.81 kB). View file
|
|
|
agent/agent.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
from .tools import tools
|
| 3 |
+
from .utils import system_prompt, handle_tool_calls
|
| 4 |
+
|
| 5 |
+
# okay now we have app file I got it from smola agent its a taste file I have agent use that agent and add that agent in app in such a way that api come with questions and this agent give answers check how app file is made you will undertand dont change structure and way and logis ogf app file
|
| 6 |
+
|
| 7 |
+
# Initialize OpenAI client
|
| 8 |
+
client = OpenAI(
|
| 9 |
+
api_key="sk-60c49cf5d82a4d4bb0a6c111eeafe941",
|
| 10 |
+
base_url="https://api.deepseek.com"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def chat_with_agent(user_message):
|
| 14 |
+
"""Main function to chat with the agent - gives short, concise responses"""
|
| 15 |
+
|
| 16 |
+
messages = [
|
| 17 |
+
{"role": "system", "content": system_prompt},
|
| 18 |
+
{"role": "user", "content": user_message}
|
| 19 |
+
]
|
| 20 |
+
|
| 21 |
+
while True:
|
| 22 |
+
# Get response from LLM with token limits
|
| 23 |
+
response = client.chat.completions.create(
|
| 24 |
+
model="deepseek-chat",
|
| 25 |
+
messages=messages,
|
| 26 |
+
tools=tools,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Add the assistant's response to messages
|
| 30 |
+
messages.append(response.choices[0].message)
|
| 31 |
+
|
| 32 |
+
# Handle tool calls if any
|
| 33 |
+
tool_results = handle_tool_calls(response)
|
| 34 |
+
|
| 35 |
+
if tool_results:
|
| 36 |
+
# Add tool results to messages
|
| 37 |
+
messages.extend(tool_results)
|
| 38 |
+
|
| 39 |
+
# Add a reminder for the final response to be short
|
| 40 |
+
messages.append({
|
| 41 |
+
"role": "system",
|
| 42 |
+
"content": "Remember: Give a SHORT summary (2-3 lines max) of the tool results. Focus on key points only."
|
| 43 |
+
})
|
| 44 |
+
|
| 45 |
+
# Continue the conversation (the LLM will respond to the tool results)
|
| 46 |
+
continue
|
| 47 |
+
else:
|
| 48 |
+
# No tool calls, return the final response
|
| 49 |
+
return response.choices[0].message.content
|
| 50 |
+
|
| 51 |
+
# def interactive_chat():
|
| 52 |
+
# """Interactive chat interface with short responses"""
|
| 53 |
+
# print("🤖 Welcome to the AI Agent!")
|
| 54 |
+
# print("I'll give you short, concise answers.")
|
| 55 |
+
# print("Type 'quit' to exit.")
|
| 56 |
+
# print("-" * 50)
|
| 57 |
+
|
| 58 |
+
# while True:
|
| 59 |
+
# user_input = input("\n👤 You: ").strip()
|
| 60 |
+
|
| 61 |
+
# if user_input.lower() in ['quit', 'exit', 'bye', 'q']:
|
| 62 |
+
# print("🤖 Goodbye!")
|
| 63 |
+
# break
|
| 64 |
+
|
| 65 |
+
# if not user_input:
|
| 66 |
+
# continue
|
| 67 |
+
|
| 68 |
+
# try:
|
| 69 |
+
# print("🤖 Thinking...")
|
| 70 |
+
# response = chat_with_agent(user_input)
|
| 71 |
+
# print(f"🤖 Assistant: {response}")
|
| 72 |
+
|
| 73 |
+
# except Exception as e:
|
| 74 |
+
# print(f"🤖 Error: {str(e)}")
|
| 75 |
+
|
| 76 |
+
# Example usage
|
| 77 |
+
# if __name__ == "__main__":
|
| 78 |
+
# # Interactive mode
|
| 79 |
+
# interactive_chat()
|
| 80 |
+
|
| 81 |
+
# Uncomment for testing with specific questions
|
| 82 |
+
# user_question = "What's the weather like in Mumbai?"
|
| 83 |
+
# print(f"User: {user_question}")
|
| 84 |
+
# answer = chat_with_agent(user_question)
|
| 85 |
+
# print(f"Assistant: {answer}")
|
| 86 |
+
|
| 87 |
+
print(chat_with_agent("tech news today"))
|
agent/handlers.py
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
from bs4 import BeautifulSoup
|
| 3 |
+
import re
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import pytz
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from io import BytesIO
|
| 8 |
+
import tempfile
|
| 9 |
+
import os
|
| 10 |
+
import speech_recognition as sr
|
| 11 |
+
from ddgs import DDGS
|
| 12 |
+
from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound
|
| 13 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 14 |
+
import torch
|
| 15 |
+
# Lazy import pydub to avoid ffmpeg warning when not needed
|
| 16 |
+
# from pydub import AudioSegment
|
| 17 |
+
# handlers for tools
|
| 18 |
+
|
| 19 |
+
# 1. get_weather
|
| 20 |
+
def get_weather(latitude : str, longitude : str) -> str:
|
| 21 |
+
"""Get current temperature for a given location using latitude and longitude coordinates."""
|
| 22 |
+
api = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m"
|
| 23 |
+
response = requests.get(api)
|
| 24 |
+
return response.json()["current"]["temperature_2m"]
|
| 25 |
+
|
| 26 |
+
# 2. web_search
|
| 27 |
+
def web_search(query: str, max_results: int = 5) -> str:
|
| 28 |
+
"""Enhanced web search with better error handling"""
|
| 29 |
+
try:
|
| 30 |
+
results_text = []
|
| 31 |
+
with DDGS() as ddgs:
|
| 32 |
+
# Add more specific search parameters
|
| 33 |
+
results = ddgs.text(
|
| 34 |
+
query,
|
| 35 |
+
max_results=max_results,
|
| 36 |
+
region='us-en',
|
| 37 |
+
safesearch='off'
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
for i, result in enumerate(results):
|
| 41 |
+
title = result.get("title", "")
|
| 42 |
+
snippet = result.get("body", "")
|
| 43 |
+
link = result.get("href", "")
|
| 44 |
+
|
| 45 |
+
# Clean up the text
|
| 46 |
+
title = title.strip() if title else ""
|
| 47 |
+
snippet = snippet.strip() if snippet else ""
|
| 48 |
+
|
| 49 |
+
if title or snippet:
|
| 50 |
+
results_text.append(f"{i+1}. {title}\n🔗 {link}\n📝 {snippet}\n")
|
| 51 |
+
|
| 52 |
+
if results_text:
|
| 53 |
+
return "\n".join(results_text)
|
| 54 |
+
else:
|
| 55 |
+
# Try alternative search approach
|
| 56 |
+
return f"No results found for: {query}. Try rephrasing your search query."
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Search error: {str(e)}. Please try a different search query."
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# 3. get_time_in_location
|
| 63 |
+
def get_current_time(timezone: str = "Asia/Kolkata") -> str:
|
| 64 |
+
"""Get current time in specified timezone."""
|
| 65 |
+
try:
|
| 66 |
+
# Convert timezone string to pytz timezone object
|
| 67 |
+
tz = pytz.timezone(timezone)
|
| 68 |
+
current_time = datetime.now(tz)
|
| 69 |
+
|
| 70 |
+
# Format the time nicely
|
| 71 |
+
formatted_time = current_time.strftime("%Y-%m-%d %H:%M:%S %Z")
|
| 72 |
+
|
| 73 |
+
return f"Current time in {timezone}: {formatted_time}"
|
| 74 |
+
|
| 75 |
+
except Exception as e:
|
| 76 |
+
return f"Error getting time: {str(e)}"
|
| 77 |
+
|
| 78 |
+
def get_time_in_location(location: str) -> str:
|
| 79 |
+
"""Get current time for a specific location."""
|
| 80 |
+
# Map common locations to timezones
|
| 81 |
+
location_timezones = {
|
| 82 |
+
"gujarat": "Asia/Kolkata",
|
| 83 |
+
"india": "Asia/Kolkata",
|
| 84 |
+
"mumbai": "Asia/Kolkata",
|
| 85 |
+
"delhi": "Asia/Kolkata",
|
| 86 |
+
"paris": "Europe/Paris",
|
| 87 |
+
"london": "Europe/London",
|
| 88 |
+
"new york": "America/New_York",
|
| 89 |
+
"tokyo": "Asia/Tokyo",
|
| 90 |
+
"sydney": "Australia/Sydney",
|
| 91 |
+
"utc": "UTC"
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
location_lower = location.lower()
|
| 95 |
+
|
| 96 |
+
if location_lower in location_timezones:
|
| 97 |
+
timezone = location_timezones[location_lower]
|
| 98 |
+
return get_current_time(timezone)
|
| 99 |
+
else:
|
| 100 |
+
return f"I don't have timezone information for '{location}'. Try: Gujarat, India, Mumbai, Delhi, Paris, London, New York, Tokyo, Sydney, or UTC."
|
| 101 |
+
|
| 102 |
+
# 4. analyze_image
|
| 103 |
+
def analyze_image(image_url: str, question: str = None) -> str:
|
| 104 |
+
"""Analyze an image and optionally answer a visual question about it."""
|
| 105 |
+
try:
|
| 106 |
+
# Download the image
|
| 107 |
+
headers = {"User-Agent": "Mozilla/5.0"}
|
| 108 |
+
response = requests.get(image_url, headers=headers, timeout=30)
|
| 109 |
+
response.raise_for_status()
|
| 110 |
+
|
| 111 |
+
# Open image
|
| 112 |
+
image = Image.open(BytesIO(response.content)).convert('RGB')
|
| 113 |
+
|
| 114 |
+
# Load BLIP model for visual QA or captioning
|
| 115 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
| 116 |
+
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-vqa-base")
|
| 117 |
+
|
| 118 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 119 |
+
model.to(device)
|
| 120 |
+
|
| 121 |
+
if question:
|
| 122 |
+
inputs = processor(image, question, return_tensors="pt").to(device)
|
| 123 |
+
out = model.generate(**inputs)
|
| 124 |
+
answer = processor.decode(out[0], skip_special_tokens=True)
|
| 125 |
+
return f"❓ Question: {question}\n🧠 Answer: {answer}"
|
| 126 |
+
else:
|
| 127 |
+
# If no question, generate a caption
|
| 128 |
+
processor_caption = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 129 |
+
model_caption = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
| 130 |
+
model_caption.to(device)
|
| 131 |
+
|
| 132 |
+
inputs = processor_caption(image, return_tensors="pt").to(device)
|
| 133 |
+
out = model_caption.generate(**inputs)
|
| 134 |
+
caption = processor_caption.decode(out[0], skip_special_tokens=True)
|
| 135 |
+
return f"🖼️ Image Caption: {caption}"
|
| 136 |
+
|
| 137 |
+
except Exception as e:
|
| 138 |
+
return f"Error analyzing image: {e}"
|
| 139 |
+
|
| 140 |
+
# 5. analyze_video
|
| 141 |
+
def analyze_video(video_url: str) -> str:
|
| 142 |
+
"""Analyze YouTube video metadata and attempt to fetch subtitles/transcript."""
|
| 143 |
+
try:
|
| 144 |
+
# Validate YouTube URL and extract video ID
|
| 145 |
+
video_id_match = re.search(r'(?:youtube\.com\/(?:watch\?v=|shorts\/)|youtu\.be\/)([^&\n?#\/]+)', video_url)
|
| 146 |
+
if not video_id_match:
|
| 147 |
+
return "Invalid YouTube URL format. Please provide a valid YouTube or Shorts URL."
|
| 148 |
+
|
| 149 |
+
video_id = video_id_match.group(1)
|
| 150 |
+
headers = {
|
| 151 |
+
"User-Agent": "Mozilla/5.0"
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
response = requests.get(video_url, headers=headers, timeout=20)
|
| 155 |
+
response.raise_for_status()
|
| 156 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 157 |
+
|
| 158 |
+
# Extract title
|
| 159 |
+
title = None
|
| 160 |
+
for selector in ['meta[property="og:title"]', 'meta[name="title"]', 'title']:
|
| 161 |
+
el = soup.select_one(selector)
|
| 162 |
+
if el:
|
| 163 |
+
title = el.get('content') or el.text
|
| 164 |
+
break
|
| 165 |
+
title_text = title.strip() if title else "Unknown title"
|
| 166 |
+
|
| 167 |
+
# Extract description
|
| 168 |
+
description = None
|
| 169 |
+
for selector in ['meta[property="og:description"]', 'meta[name="description"]']:
|
| 170 |
+
el = soup.select_one(selector)
|
| 171 |
+
if el:
|
| 172 |
+
description = el.get('content') or el.text
|
| 173 |
+
break
|
| 174 |
+
desc_text = description.strip() if description else "No description available"
|
| 175 |
+
|
| 176 |
+
# Extract channel
|
| 177 |
+
channel = None
|
| 178 |
+
for selector in ['link[itemprop="name"]', 'meta[itemprop="channelId"]', 'a[href*="/@"]']:
|
| 179 |
+
el = soup.select_one(selector)
|
| 180 |
+
if el:
|
| 181 |
+
channel = el.get('content') or el.text or el.get('title')
|
| 182 |
+
break
|
| 183 |
+
channel_text = channel.strip() if channel else "Unknown channel"
|
| 184 |
+
|
| 185 |
+
# Attempt to fetch transcript using YouTubeTranscriptApi
|
| 186 |
+
transcript_text = "Transcript not available."
|
| 187 |
+
try:
|
| 188 |
+
transcript = YouTubeTranscriptApi.get_transcript(video_id)
|
| 189 |
+
transcript_text = "\n".join([f"{line['text']}" for line in transcript[:5]]) + "\n..."
|
| 190 |
+
except (TranscriptsDisabled, NoTranscriptFound):
|
| 191 |
+
transcript_text = "No transcript available for this video."
|
| 192 |
+
except Exception as e:
|
| 193 |
+
transcript_text = f"Transcript fetch failed: {e}"
|
| 194 |
+
|
| 195 |
+
# Final analysis summary
|
| 196 |
+
result = f"📺 Video Analysis\n"
|
| 197 |
+
result += f"Title: {title_text}\n"
|
| 198 |
+
result += f"Channel: {channel_text}\n"
|
| 199 |
+
result += f"Description: {desc_text[:300]}...\n"
|
| 200 |
+
result += f"Transcript Preview:\n{transcript_text}"
|
| 201 |
+
|
| 202 |
+
return result
|
| 203 |
+
|
| 204 |
+
except Exception as e:
|
| 205 |
+
return f"Error analyzing video: {e}"
|
| 206 |
+
|
| 207 |
+
# 6. transcribe_audio
|
| 208 |
+
def transcribe_audio(audio_url: str) -> str:
|
| 209 |
+
"""Transcribe audio content from an audio file URL using speech recognition."""
|
| 210 |
+
try:
|
| 211 |
+
# Download the audio file
|
| 212 |
+
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
|
| 213 |
+
response = requests.get(audio_url, headers=headers, timeout=30)
|
| 214 |
+
response.raise_for_status()
|
| 215 |
+
|
| 216 |
+
# Create a temporary file to store the audio
|
| 217 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as temp_audio:
|
| 218 |
+
temp_audio.write(response.content)
|
| 219 |
+
temp_audio_path = temp_audio.name
|
| 220 |
+
|
| 221 |
+
try:
|
| 222 |
+
# Try to convert audio to WAV format (speech_recognition works better with WAV)
|
| 223 |
+
try:
|
| 224 |
+
# Lazy import pydub only when needed
|
| 225 |
+
from pydub import AudioSegment
|
| 226 |
+
audio = AudioSegment.from_file(temp_audio_path)
|
| 227 |
+
|
| 228 |
+
# Create temporary WAV file
|
| 229 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_wav:
|
| 230 |
+
audio.export(temp_wav.name, format="wav")
|
| 231 |
+
wav_path = temp_wav.name
|
| 232 |
+
|
| 233 |
+
except Exception as e:
|
| 234 |
+
# If conversion fails, try using the original file directly
|
| 235 |
+
print(f"Audio conversion warning: {e}")
|
| 236 |
+
wav_path = temp_audio_path
|
| 237 |
+
|
| 238 |
+
# Initialize speech recognizer
|
| 239 |
+
recognizer = sr.Recognizer()
|
| 240 |
+
|
| 241 |
+
# Load the audio file
|
| 242 |
+
with sr.AudioFile(wav_path) as source:
|
| 243 |
+
# Adjust for ambient noise
|
| 244 |
+
recognizer.adjust_for_ambient_noise(source, duration=0.5)
|
| 245 |
+
# Record the audio
|
| 246 |
+
audio_data = recognizer.record(source)
|
| 247 |
+
|
| 248 |
+
# Try to recognize speech
|
| 249 |
+
try:
|
| 250 |
+
# Use Google Speech Recognition (free, requires internet)
|
| 251 |
+
text = recognizer.recognize_google(audio_data)
|
| 252 |
+
return f"Audio Transcription:\n{text}"
|
| 253 |
+
|
| 254 |
+
except sr.UnknownValueError:
|
| 255 |
+
return "Audio Transcription: Could not understand the audio. The speech may be unclear, too quiet, or in a language not supported."
|
| 256 |
+
|
| 257 |
+
except sr.RequestError as e:
|
| 258 |
+
return f"Audio Transcription: Error with speech recognition service: {str(e)}"
|
| 259 |
+
|
| 260 |
+
finally:
|
| 261 |
+
# Clean up temporary files
|
| 262 |
+
try:
|
| 263 |
+
os.unlink(temp_audio_path)
|
| 264 |
+
if wav_path != temp_audio_path: # Only delete wav file if it's different
|
| 265 |
+
os.unlink(wav_path)
|
| 266 |
+
except:
|
| 267 |
+
pass
|
| 268 |
+
|
| 269 |
+
except ImportError:
|
| 270 |
+
return "Audio Transcription: Speech recognition library not available. Please install 'SpeechRecognition' package."
|
| 271 |
+
|
| 272 |
+
except Exception as e:
|
| 273 |
+
return f"Audio Transcription Error: {str(e)}"
|
agent/tempCodeRunnerFile.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
handle_tool_calls
|
agent/tools.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tools = [{
|
| 2 |
+
"type": "function",
|
| 3 |
+
"function": {
|
| 4 |
+
"name": "get_weather",
|
| 5 |
+
"description": "Get current temperature for a given location using latitude and longitude coordinates.",
|
| 6 |
+
"parameters": {
|
| 7 |
+
"type": "object",
|
| 8 |
+
"properties": {
|
| 9 |
+
"latitude": {
|
| 10 |
+
"type": "string",
|
| 11 |
+
"description": "Latitude coordinate (e.g., '23.0225')"
|
| 12 |
+
},
|
| 13 |
+
"longitude": {
|
| 14 |
+
"type": "string",
|
| 15 |
+
"description": "Longitude coordinate (e.g., '72.5714')"
|
| 16 |
+
}
|
| 17 |
+
},
|
| 18 |
+
"required": [
|
| 19 |
+
"latitude",
|
| 20 |
+
"longitude"
|
| 21 |
+
],
|
| 22 |
+
"additionalProperties": False
|
| 23 |
+
}
|
| 24 |
+
}
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"type": "function",
|
| 28 |
+
"function": {
|
| 29 |
+
"name": "web_search",
|
| 30 |
+
"description": "Go to web and take information regarding the user query. always return the up to date real time information.",
|
| 31 |
+
"parameters": {
|
| 32 |
+
"type": "object",
|
| 33 |
+
"properties": {
|
| 34 |
+
"query": {
|
| 35 |
+
"type": "string",
|
| 36 |
+
"description": "Search query for web search"
|
| 37 |
+
}
|
| 38 |
+
},
|
| 39 |
+
"required": [
|
| 40 |
+
"query"
|
| 41 |
+
],
|
| 42 |
+
"additionalProperties": False
|
| 43 |
+
}
|
| 44 |
+
}
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"type": "function",
|
| 48 |
+
"function": {
|
| 49 |
+
"name": "get_time_in_location",
|
| 50 |
+
"description": "Get current time for a specific location or city.",
|
| 51 |
+
"parameters": {
|
| 52 |
+
"type": "object",
|
| 53 |
+
"properties": {
|
| 54 |
+
"location": {
|
| 55 |
+
"type": "string",
|
| 56 |
+
"description": "Location name (e.g., 'Gujarat', 'Mumbai', 'Paris', 'New York')"
|
| 57 |
+
}
|
| 58 |
+
},
|
| 59 |
+
"required": ["location"],
|
| 60 |
+
"additionalProperties": False
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
},
|
| 64 |
+
{
|
| 65 |
+
"type": "function",
|
| 66 |
+
"function": {
|
| 67 |
+
"name": "analyze_image",
|
| 68 |
+
"description": "Describe what is visible in an image - people, objects, animals, scenes, actions, and what is happening. Focus on content description rather than technical properties. Use this when users ask about what they see in an image or provide image URLs.",
|
| 69 |
+
"parameters": {
|
| 70 |
+
"type": "object",
|
| 71 |
+
"properties": {
|
| 72 |
+
"image_url": {
|
| 73 |
+
"type": "string",
|
| 74 |
+
"description": "URL of the image to analyze (supports common formats like JPG, PNG, etc.)"
|
| 75 |
+
}
|
| 76 |
+
},
|
| 77 |
+
"required": ["image_url"]
|
| 78 |
+
}
|
| 79 |
+
}
|
| 80 |
+
},
|
| 81 |
+
{
|
| 82 |
+
"type": "function",
|
| 83 |
+
"function": {
|
| 84 |
+
"name": "analyze_video",
|
| 85 |
+
"description": "Use this when users ask about video content and provide YouTube URLs or just youtube video url. give the title, description, channel, and transcript of the video or if ask something than just give strightforward answer of that question",
|
| 86 |
+
"parameters": {
|
| 87 |
+
"type": "object",
|
| 88 |
+
"properties": {
|
| 89 |
+
"video_url": {
|
| 90 |
+
"type": "string",
|
| 91 |
+
"description": "YouTube URL of the video to analyze (e.g., https://www.youtube.com/watch?v=..., https://youtu.be/..., or https://www.youtube.com/shorts/...)"
|
| 92 |
+
}
|
| 93 |
+
},
|
| 94 |
+
"required": ["video_url"]
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
},
|
| 98 |
+
{
|
| 99 |
+
"type": "function",
|
| 100 |
+
"function": {
|
| 101 |
+
"name": "transcribe_audio",
|
| 102 |
+
"description": "Transcribe audio content from an audio file URL. Extract spoken words and convert them to text. Use this when users provide audio files (.mp3, .wav, .m4a, etc.) or ask about audio content.",
|
| 103 |
+
"parameters": {
|
| 104 |
+
"type": "object",
|
| 105 |
+
"properties": {
|
| 106 |
+
"audio_url": {
|
| 107 |
+
"type": "string",
|
| 108 |
+
"description": "URL of the audio file to transcribe (supports common formats like MP3, WAV, M4A, etc.)"
|
| 109 |
+
}
|
| 110 |
+
},
|
| 111 |
+
"required": ["audio_url"]
|
| 112 |
+
}
|
| 113 |
+
}
|
| 114 |
+
}
|
| 115 |
+
]
|
agent/utils.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
from .handlers import get_weather, web_search, get_time_in_location, analyze_image, analyze_video, transcribe_audio
|
| 3 |
+
|
| 4 |
+
# loop for tool calls
|
| 5 |
+
def handle_tool_calls(response):
|
| 6 |
+
"""Handle tool calls from the LLM response"""
|
| 7 |
+
message = response.choices[0].message
|
| 8 |
+
|
| 9 |
+
# If the LLM wants to use tools
|
| 10 |
+
if message.tool_calls:
|
| 11 |
+
# Create a list to store tool results
|
| 12 |
+
tool_results = []
|
| 13 |
+
|
| 14 |
+
for tool_call in message.tool_calls:
|
| 15 |
+
function_name = tool_call.function.name
|
| 16 |
+
function_args = json.loads(tool_call.function.arguments)
|
| 17 |
+
|
| 18 |
+
# print(f"🔧 Calling function: {function_name}")
|
| 19 |
+
# print(f"📝 With arguments: {function_args}")
|
| 20 |
+
|
| 21 |
+
# Call the appropriate function based on the tool name
|
| 22 |
+
if function_name == "get_weather":
|
| 23 |
+
result = get_weather(
|
| 24 |
+
function_args["latitude"],
|
| 25 |
+
function_args["longitude"]
|
| 26 |
+
)
|
| 27 |
+
elif function_name == "web_search":
|
| 28 |
+
result = web_search(function_args["query"])
|
| 29 |
+
elif function_name == "get_time_in_location":
|
| 30 |
+
result = get_time_in_location(function_args["location"])
|
| 31 |
+
elif function_name == "analyze_image":
|
| 32 |
+
result = analyze_image(function_args["image_url"])
|
| 33 |
+
elif function_name == "analyze_video":
|
| 34 |
+
result = analyze_video(function_args["video_url"])
|
| 35 |
+
elif function_name == "transcribe_audio":
|
| 36 |
+
result = transcribe_audio(function_args["audio_url"])
|
| 37 |
+
else:
|
| 38 |
+
result = f"Unknown function: {function_name}"
|
| 39 |
+
|
| 40 |
+
# Add the result to our list
|
| 41 |
+
tool_results.append({
|
| 42 |
+
"tool_call_id": tool_call.id,
|
| 43 |
+
"role": "tool",
|
| 44 |
+
"content": str(result)
|
| 45 |
+
})
|
| 46 |
+
|
| 47 |
+
return tool_results
|
| 48 |
+
|
| 49 |
+
return None
|
| 50 |
+
|
| 51 |
+
# Enhanced system prompt for short responses
|
| 52 |
+
# system_prompt = """You are a powerful and well knowledgeable AI assistant with access to weather, time, web search, video analysis, and audio transcription tools. If someone asks you about anything you can answer it with or without the help of tools. You are also a good and friendly assistant. There is nothing you don't know - always give answers, never say you don't know.
|
| 53 |
+
|
| 54 |
+
# IMPORTANT RESPONSE RULES:
|
| 55 |
+
# - Keep responses SHORT and CONCISE (2-3 lines maximum)
|
| 56 |
+
# - Focus only on the MOST IMPORTANT information
|
| 57 |
+
# - Use bullet points or short sentences
|
| 58 |
+
# - Avoid long explanations and paragraphs
|
| 59 |
+
# - Be direct and to the point
|
| 60 |
+
# - If using tools, summarize the key findings briefly
|
| 61 |
+
|
| 62 |
+
# For VIDEO ANALYSIS:
|
| 63 |
+
# - When given a YouTube URL, use the analyze_video tool to extract video information
|
| 64 |
+
# - Summarize the video content based on title, description, and channel info
|
| 65 |
+
# - Focus on what the video is about, not generic YouTube Shorts descriptions
|
| 66 |
+
|
| 67 |
+
# For AUDIO TRANSCRIPTION:
|
| 68 |
+
# - When given an audio file URL (.mp3, .wav, etc.), use the transcribe_audio tool
|
| 69 |
+
# - Extract the spoken content and provide a clear summary
|
| 70 |
+
# - Focus on the key information requested from the audio
|
| 71 |
+
|
| 72 |
+
# Example good response:
|
| 73 |
+
# "Current temperature in Mumbai: 28°C. Weather is sunny with light winds."
|
| 74 |
+
|
| 75 |
+
# Example bad response:
|
| 76 |
+
# "The weather in Mumbai is currently quite pleasant with a temperature of 28 degrees Celsius. The conditions are sunny with some light winds blowing through the city, making it a perfect day for outdoor activities..."
|
| 77 |
+
|
| 78 |
+
# When using web search results, extract only the key points and present them concisely.
|
| 79 |
+
# """
|
| 80 |
+
|
| 81 |
+
# ... existing code ...
|
| 82 |
+
system_prompt = """You are a powerful AI assistant with access to specialized tools for:
|
| 83 |
+
- YouTube video analysis (extract title, description, channel, transcripts)
|
| 84 |
+
- Image analysis (describe images and answer visual questions)
|
| 85 |
+
- Web search (get real-time information)
|
| 86 |
+
- Audio transcription (convert speech to text)
|
| 87 |
+
- Weather data (get current temperature)
|
| 88 |
+
- Time information (get time for different locations)
|
| 89 |
+
|
| 90 |
+
IMPORTANT: When users ask about videos, images, audio, or need current information, USE THE APPROPRIATE TOOLS to provide accurate, up-to-date answers.
|
| 91 |
+
|
| 92 |
+
For video analysis: Use analyze_video tool for YouTube URLs
|
| 93 |
+
For image analysis: Use analyze_image tool for image URLs
|
| 94 |
+
For web search: Use web_search tool for current information
|
| 95 |
+
For audio: Use transcribe_audio tool for audio files
|
| 96 |
+
|
| 97 |
+
Always provide helpful, detailed responses using your tools when appropriate. If you need to use tools, do so and then provide a comprehensive answer based on the results.
|
| 98 |
+
|
| 99 |
+
FINAL ANSWER: [Your final answer based on tool results or knowledge]"""
|