Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,10 @@ from langchain_openai import ChatOpenAI
|
|
| 10 |
from langchain_community.tools import DuckDuckGoSearchResults
|
| 11 |
from langchain_google_community import GoogleSearchAPIWrapper
|
| 12 |
import wikipedia
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# (Keep Constants as is)
|
| 14 |
# --- Constants ---
|
| 15 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
@@ -34,13 +38,20 @@ def add(a: int|float, b: int|float) -> float:
|
|
| 34 |
def subtract(a: int|float, b: int|float) -> float:
|
| 35 |
"""Subtract a with b."""
|
| 36 |
return a - b
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
def search_wikipedia(query: str) -> str:
|
| 39 |
-
"""Search English Wikipedia for the given query and return a summary within
|
| 40 |
try:
|
| 41 |
-
return wikipedia.summary(query, sentences=
|
| 42 |
except wikipedia.DisambiguationError as e:
|
| 43 |
-
return f"Ambiguous query. Options: {e.options[:
|
| 44 |
except Exception as e:
|
| 45 |
return f"Error: {str(e)}"
|
| 46 |
|
|
@@ -77,15 +88,16 @@ from langchain_core.messages import HumanMessage, SystemMessage
|
|
| 77 |
def assistant(state: AgentState, llm_with_tools):
|
| 78 |
# System message
|
| 79 |
textual_description_of_tool = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
search_wikipedia(query: str) -> str:
|
| 81 |
Search English Wikipedia for the given query and return a summary within 3 sentences.
|
| 82 |
Returns:
|
| 83 |
-
|
| 84 |
-
return wikipedia.summary(query, sentences=3)
|
| 85 |
-
except wikipedia.DisambiguationError as e:
|
| 86 |
-
return f"Ambiguous query. Options: {e.options[:5]}"
|
| 87 |
-
except Exception as e:
|
| 88 |
-
return f"Error: {str(e)}"
|
| 89 |
|
| 90 |
GoogleSearchAPIWrapper(k=10).run():
|
| 91 |
Search for Top k = 10 information in the internet through GoogleSearch engine
|
|
|
|
| 10 |
from langchain_community.tools import DuckDuckGoSearchResults
|
| 11 |
from langchain_google_community import GoogleSearchAPIWrapper
|
| 12 |
import wikipedia
|
| 13 |
+
import speech_recognition as sr
|
| 14 |
+
# Or using AudioTranscriptTool.
|
| 15 |
+
|
| 16 |
+
|
| 17 |
# (Keep Constants as is)
|
| 18 |
# --- Constants ---
|
| 19 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
|
|
| 38 |
def subtract(a: int|float, b: int|float) -> float:
|
| 39 |
"""Subtract a with b."""
|
| 40 |
return a - b
|
| 41 |
+
|
| 42 |
+
def transcribe_audio(file_path: str) -> str:
|
| 43 |
+
"""Transcribe audio file using SpeechRecognition."""
|
| 44 |
+
r = sr.Recognizer()
|
| 45 |
+
with sr.AudioFile(file_path) as source:
|
| 46 |
+
audio = r.record(source)
|
| 47 |
+
return r.recognize_google(audio)
|
| 48 |
+
|
| 49 |
def search_wikipedia(query: str) -> str:
|
| 50 |
+
"""Search English Wikipedia for the given query and return a summary within 10 sentences."""
|
| 51 |
try:
|
| 52 |
+
return wikipedia.summary(query, sentences=10)
|
| 53 |
except wikipedia.DisambiguationError as e:
|
| 54 |
+
return f"Ambiguous query. Options: {e.options[:10]}"
|
| 55 |
except Exception as e:
|
| 56 |
return f"Error: {str(e)}"
|
| 57 |
|
|
|
|
| 88 |
def assistant(state: AgentState, llm_with_tools):
|
| 89 |
# System message
|
| 90 |
textual_description_of_tool = """
|
| 91 |
+
|
| 92 |
+
transcribe_audio(file_path: str) -> str:
|
| 93 |
+
Transcribe audio file using SpeechRecognition.
|
| 94 |
+
Returns:
|
| 95 |
+
The transcribed text from the given audio.
|
| 96 |
+
|
| 97 |
search_wikipedia(query: str) -> str:
|
| 98 |
Search English Wikipedia for the given query and return a summary within 3 sentences.
|
| 99 |
Returns:
|
| 100 |
+
The searched content in wikipedia or exception information.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
GoogleSearchAPIWrapper(k=10).run():
|
| 103 |
Search for Top k = 10 information in the internet through GoogleSearch engine
|