Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ import pytesseract
|
|
| 7 |
from pydub import AudioSegment
|
| 8 |
import yt_dlp
|
| 9 |
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
| 10 |
|
| 11 |
from openai import OpenAI
|
| 12 |
|
|
@@ -64,26 +66,75 @@ def execute_tool(self, tool, input_data, file_url):
|
|
| 64 |
if tool == "calculator":
|
| 65 |
|
| 66 |
return str(eval(input_data, {"__builtins__": {}}))
|
| 67 |
-
|
|
|
|
| 68 |
if tool == "wiki_search":
|
| 69 |
|
| 70 |
return self.wiki_search(input_data)
|
| 71 |
|
| 72 |
return "Unknown tool"
|
| 73 |
|
| 74 |
-
def
|
|
|
|
| 75 |
try:
|
| 76 |
-
ydl_opts = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 78 |
info = ydl.extract_info(url, download=False)
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
# --- Basic Agent Definition ---
|
| 83 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 84 |
from openai import OpenAI
|
| 85 |
|
| 86 |
class BasicAgent:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
def agent_loop(self, question, file_url):
|
| 88 |
|
| 89 |
memory = ""
|
|
@@ -95,11 +146,12 @@ class BasicAgent:
|
|
| 95 |
|
| 96 |
Available tools:
|
| 97 |
|
| 98 |
-
1. read_excel
|
| 99 |
-
2. read_image
|
| 100 |
-
3. scrape_page
|
| 101 |
-
4.
|
| 102 |
-
5.
|
|
|
|
| 103 |
|
| 104 |
Question:
|
| 105 |
{question}
|
|
@@ -301,7 +353,8 @@ class BasicAgent:
|
|
| 301 |
if file.endswith(".png") or file.endswith(".jpg"):
|
| 302 |
|
| 303 |
return self.read_image(file)
|
| 304 |
-
|
|
|
|
| 305 |
if file.endswith(".py"):
|
| 306 |
|
| 307 |
with open(file) as f:
|
|
@@ -409,7 +462,11 @@ Return only the answer.
|
|
| 409 |
# ------------------------------------------------
|
| 410 |
|
| 411 |
def __call__(self, question, file_url=None):
|
| 412 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 413 |
print("Question:", question)
|
| 414 |
|
| 415 |
answer = self.agent_loop(question, file_url)
|
|
|
|
| 7 |
from pydub import AudioSegment
|
| 8 |
import yt_dlp
|
| 9 |
from bs4 import BeautifulSoup
|
| 10 |
+
import whisper
|
| 11 |
+
import yt_dlp
|
| 12 |
|
| 13 |
from openai import OpenAI
|
| 14 |
|
|
|
|
| 66 |
if tool == "calculator":
|
| 67 |
|
| 68 |
return str(eval(input_data, {"__builtins__": {}}))
|
| 69 |
+
if tool == "youtube_captions":
|
| 70 |
+
return self.youtube_captions(input_data)
|
| 71 |
if tool == "wiki_search":
|
| 72 |
|
| 73 |
return self.wiki_search(input_data)
|
| 74 |
|
| 75 |
return "Unknown tool"
|
| 76 |
|
| 77 |
+
def youtube_captions(self, url):
|
| 78 |
+
|
| 79 |
try:
|
| 80 |
+
ydl_opts = {
|
| 81 |
+
"skip_download": True,
|
| 82 |
+
"writesubtitles": True,
|
| 83 |
+
"writeautomaticsub": True,
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 87 |
info = ydl.extract_info(url, download=False)
|
| 88 |
+
|
| 89 |
+
captions = info.get("subtitles") or info.get("automatic_captions")
|
| 90 |
+
|
| 91 |
+
return str(captions)
|
| 92 |
+
|
| 93 |
+
except Exception as e:
|
| 94 |
+
print("YouTube error:", e)
|
| 95 |
+
return ""
|
| 96 |
# --- Basic Agent Definition ---
|
| 97 |
# ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
|
| 98 |
from openai import OpenAI
|
| 99 |
|
| 100 |
class BasicAgent:
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def read_audio(self, file):
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
model = whisper.load_model("base")
|
| 107 |
+
|
| 108 |
+
result = model.transcribe(file)
|
| 109 |
+
|
| 110 |
+
return result["text"]
|
| 111 |
+
|
| 112 |
+
except:
|
| 113 |
+
return ""
|
| 114 |
+
def execute_tool(self, tool, input_data, file_url):
|
| 115 |
+
|
| 116 |
+
if tool == "wiki_search":
|
| 117 |
+
return self.wiki_search(input_data)
|
| 118 |
+
|
| 119 |
+
if tool == "scrape_page":
|
| 120 |
+
return self.scrape_page(input_data)
|
| 121 |
+
|
| 122 |
+
if tool == "read_excel":
|
| 123 |
+
file = self.download_file(file_url)
|
| 124 |
+
return self.read_excel(file)
|
| 125 |
+
|
| 126 |
+
if tool == "read_image":
|
| 127 |
+
file = self.download_file(file_url)
|
| 128 |
+
return self.read_image(file)
|
| 129 |
+
|
| 130 |
+
if tool == "calculator":
|
| 131 |
+
try:
|
| 132 |
+
return str(eval(input_data))
|
| 133 |
+
except:
|
| 134 |
+
return "error"
|
| 135 |
+
|
| 136 |
+
return "unknown tool"
|
| 137 |
+
|
| 138 |
def agent_loop(self, question, file_url):
|
| 139 |
|
| 140 |
memory = ""
|
|
|
|
| 146 |
|
| 147 |
Available tools:
|
| 148 |
|
| 149 |
+
1. read_excel(file_url)
|
| 150 |
+
2. read_image(file_url)
|
| 151 |
+
3. scrape_page(url)
|
| 152 |
+
4. youtube_captions(url)
|
| 153 |
+
5. calculator(expression)
|
| 154 |
+
6. wiki_search(query)
|
| 155 |
|
| 156 |
Question:
|
| 157 |
{question}
|
|
|
|
| 353 |
if file.endswith(".png") or file.endswith(".jpg"):
|
| 354 |
|
| 355 |
return self.read_image(file)
|
| 356 |
+
if file.endswith(".mp3"):
|
| 357 |
+
return self.read_audio(file)
|
| 358 |
if file.endswith(".py"):
|
| 359 |
|
| 360 |
with open(file) as f:
|
|
|
|
| 462 |
# ------------------------------------------------
|
| 463 |
|
| 464 |
def __call__(self, question, file_url=None):
|
| 465 |
+
if "youtube.com" in question or "youtu.be" in question:
|
| 466 |
+
captions = self.youtube_captions(question)
|
| 467 |
+
file_content = captions
|
| 468 |
+
else:
|
| 469 |
+
file_content = self.load_file(file_url)
|
| 470 |
print("Question:", question)
|
| 471 |
|
| 472 |
answer = self.agent_loop(question, file_url)
|