Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,9 +9,12 @@ from langchain_core.messages import HumanMessage
|
|
| 9 |
from langchain_openai import ChatOpenAI
|
| 10 |
from langchain_community.tools import DuckDuckGoSearchResults
|
| 11 |
from langchain_google_community import GoogleSearchAPIWrapper
|
|
|
|
| 12 |
import wikipedia
|
| 13 |
import speech_recognition as sr
|
| 14 |
import tempfile
|
|
|
|
|
|
|
| 15 |
# Or using AudioTranscriptTool.
|
| 16 |
|
| 17 |
|
|
@@ -39,7 +42,33 @@ def add(a: int|float, b: int|float) -> float:
|
|
| 39 |
def subtract(a: int|float, b: int|float) -> float:
|
| 40 |
"""Subtract a with b."""
|
| 41 |
return a - b
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
| 43 |
def transcribe_audio(file_url: str) -> str:
|
| 44 |
"""Downloads an audio file from a URL into a temporary file and transcribes it using SpeechRecognition."""
|
| 45 |
try:
|
|
@@ -75,6 +104,9 @@ tools = [
|
|
| 75 |
multiply,
|
| 76 |
add,
|
| 77 |
subtract,
|
|
|
|
|
|
|
|
|
|
| 78 |
search_wikipedia,
|
| 79 |
GoogleSearchAPIWrapper(k=10).run,
|
| 80 |
DuckDuckGoSearchResults(),
|
|
@@ -86,9 +118,9 @@ from langgraph.graph.message import add_messages
|
|
| 86 |
|
| 87 |
|
| 88 |
class AgentState(TypedDict):
|
| 89 |
-
# The input document
|
| 90 |
messages: Annotated[list[AnyMessage], add_messages]
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
from langchain_core.messages import HumanMessage, SystemMessage
|
| 94 |
|
|
@@ -110,6 +142,20 @@ transcribe_audio(file_url: str) -> str:
|
|
| 110 |
Returns:
|
| 111 |
The transcribed text from the given audio.
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
search_wikipedia(query: str) -> str:
|
| 114 |
Search English Wikipedia for the given query and return a summary within 3 sentences.
|
| 115 |
Returns:
|
|
|
|
| 9 |
from langchain_openai import ChatOpenAI
|
| 10 |
from langchain_community.tools import DuckDuckGoSearchResults
|
| 11 |
from langchain_google_community import GoogleSearchAPIWrapper
|
| 12 |
+
from langchain_community.document_loaders import YoutubeLoader
|
| 13 |
import wikipedia
|
| 14 |
import speech_recognition as sr
|
| 15 |
import tempfile
|
| 16 |
+
import ast
|
| 17 |
+
|
| 18 |
# Or using AudioTranscriptTool.
|
| 19 |
|
| 20 |
|
|
|
|
| 42 |
def subtract(a: int|float, b: int|float) -> float:
|
| 43 |
"""Subtract a with b."""
|
| 44 |
return a - b
|
| 45 |
+
|
| 46 |
+
def youtube_transcript_tool(url: str) -> str:
|
| 47 |
+
"""Load transcript from a YouTube video URL."""
|
| 48 |
+
loader = YoutubeLoader.from_youtube_url(url, add_video_info=True)
|
| 49 |
+
docs = loader.load()
|
| 50 |
+
return "\n".join([f"Title: {doc.metadata.get('title', 'Unknown')}\nTranscript: {doc.page_content}" for doc in docs])
|
| 51 |
+
|
| 52 |
+
def analyze_python_code(file_url: str) -> str:
|
| 53 |
+
"""Downloads an python code from a URL, then analyze it and summarize its structure."""
|
| 54 |
+
try:
|
| 55 |
+
# Download file into temporary file
|
| 56 |
+
with tempfile.NamedTemporaryFile(suffix=".py", delete=True) as temp_file:
|
| 57 |
+
response = requests.get(file_url)
|
| 58 |
+
if response.status_code != 200:
|
| 59 |
+
return f"Failed to download file: {response.status_code}"
|
| 60 |
+
|
| 61 |
+
temp_file.write(response.content)
|
| 62 |
+
temp_file.flush() # Make sure data is written
|
| 63 |
+
# Transcribe from temp file
|
| 64 |
+
tree = ast.parse(temp_file.name)
|
| 65 |
+
functions = [node.name for node in ast.walk(tree) if isinstance(node, ast.FunctionDef)]
|
| 66 |
+
classes = [node.name for node in ast.walk(tree) if isinstance(node, ast.ClassDef)]
|
| 67 |
+
return f"Functions: {functions}\nClasses: {classes}"
|
| 68 |
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return f"Reading failed: {str(e)}"
|
| 71 |
+
|
| 72 |
def transcribe_audio(file_url: str) -> str:
|
| 73 |
"""Downloads an audio file from a URL into a temporary file and transcribes it using SpeechRecognition."""
|
| 74 |
try:
|
|
|
|
| 104 |
multiply,
|
| 105 |
add,
|
| 106 |
subtract,
|
| 107 |
+
youtube_transcript_tool,
|
| 108 |
+
transcribe_audio,
|
| 109 |
+
analyze_python_code,
|
| 110 |
search_wikipedia,
|
| 111 |
GoogleSearchAPIWrapper(k=10).run,
|
| 112 |
DuckDuckGoSearchResults(),
|
|
|
|
| 118 |
|
| 119 |
|
| 120 |
class AgentState(TypedDict):
|
|
|
|
| 121 |
messages: Annotated[list[AnyMessage], add_messages]
|
| 122 |
+
# The input file download URL
|
| 123 |
+
input_file: Optional[str]
|
| 124 |
|
| 125 |
from langchain_core.messages import HumanMessage, SystemMessage
|
| 126 |
|
|
|
|
| 142 |
Returns:
|
| 143 |
The transcribed text from the given audio.
|
| 144 |
|
| 145 |
+
youtube_transcript_tool(url: str) -> str:
|
| 146 |
+
Load transcript from a YouTube video URL.
|
| 147 |
+
Args:
|
| 148 |
+
url, a string indicating the url of the given youtube video.
|
| 149 |
+
Returns:
|
| 150 |
+
The transcript of the selected youtube video.
|
| 151 |
+
|
| 152 |
+
analyze_python_code(file_url: str) -> str:
|
| 153 |
+
Downloads an python code from a URL, then analyze it and summarize its structure.
|
| 154 |
+
Args:
|
| 155 |
+
file_url, a string indicating the url of the given file.
|
| 156 |
+
Returns:
|
| 157 |
+
The structure and functions of the given code.
|
| 158 |
+
|
| 159 |
search_wikipedia(query: str) -> str:
|
| 160 |
Search English Wikipedia for the given query and return a summary within 3 sentences.
|
| 161 |
Returns:
|