Spaces:
Runtime error
Runtime error
Commit
·
8d59b1d
1
Parent(s):
42f3ed0
Frist Commit
Browse files- agents.py +32 -0
- app.py +46 -0
- interface.py +27 -0
- requirements.txt +5 -0
agents.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.chat_models import ChatOpenAI
|
2 |
+
from langchain.agents import (
|
3 |
+
AgentType,
|
4 |
+
load_tools,
|
5 |
+
initialize_agent
|
6 |
+
)
|
7 |
+
from langchain.memory import ConversationBufferMemory
|
8 |
+
from langchain.callbacks import StdOutCallbackHandler
|
9 |
+
|
10 |
+
class SmartChatAgent:
|
11 |
+
|
12 |
+
def __init__(self) -> None:
|
13 |
+
|
14 |
+
self.memory = ConversationBufferMemory(
|
15 |
+
memory_key="chat_history",
|
16 |
+
return_messages=True
|
17 |
+
)
|
18 |
+
|
19 |
+
self.llm = ChatOpenAI()
|
20 |
+
self.tools = load_tools(['google-search'])
|
21 |
+
|
22 |
+
self.agent = initialize_agent(
|
23 |
+
self.tools,
|
24 |
+
self.llm,
|
25 |
+
agent=AgentType.CHAT_CONVERSATIONAL_REACT_DESCRIPTION,
|
26 |
+
memory=self.memory,
|
27 |
+
verbose=True,
|
28 |
+
)
|
29 |
+
|
30 |
+
def run(self, text):
|
31 |
+
handler = StdOutCallbackHandler()
|
32 |
+
return self.agent.run(text, callbacks=[handler])
|
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import openai
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
load_dotenv()
|
6 |
+
openai.api_key=os.environ["OPENAI_API_KEY"]
|
7 |
+
|
8 |
+
import gradio as gr
|
9 |
+
from langchain.llms import OpenAI
|
10 |
+
from interface import AudioInterface
|
11 |
+
interface = AudioInterface()
|
12 |
+
|
13 |
+
|
14 |
+
def process(filepath):
|
15 |
+
audio = open(filepath,"rb")
|
16 |
+
transcript = openai.Audio.transcribe("whisper-1",audio)
|
17 |
+
llm = OpenAI(temperature=1)
|
18 |
+
print(llm(transcript["text"]))
|
19 |
+
interface.speak(llm(transcript["text"]))
|
20 |
+
return llm(transcript["text"])
|
21 |
+
|
22 |
+
demo = gr.Interface(
|
23 |
+
fn=process,
|
24 |
+
inputs=gr.Audio(source="microphone",type="filepath"),
|
25 |
+
outputs="text")
|
26 |
+
demo.launch()
|
27 |
+
|
28 |
+
|
29 |
+
"""
|
30 |
+
|
31 |
+
from dotenv import load_dotenv
|
32 |
+
load_dotenv()
|
33 |
+
|
34 |
+
from interface import AudioInterface
|
35 |
+
from agents import SmartChatAgent
|
36 |
+
|
37 |
+
interface = AudioInterface()
|
38 |
+
agent = SmartChatAgent()
|
39 |
+
|
40 |
+
while True:
|
41 |
+
text = interface.listen()
|
42 |
+
response = agent.run(text)
|
43 |
+
interface.speak(response)
|
44 |
+
|
45 |
+
|
46 |
+
"""
|
interface.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import speech_recognition as sr
|
3 |
+
from elevenlabs import generate, play, set_api_key
|
4 |
+
set_api_key(os.environ['ELEVEN_API_KEY'])
|
5 |
+
|
6 |
+
class AudioInterface:
|
7 |
+
|
8 |
+
def listen(self) -> str:
|
9 |
+
recognizer = sr.Recognizer()
|
10 |
+
with sr.Microphone() as source:
|
11 |
+
print("Say something!")
|
12 |
+
audio = recognizer.listen(source)
|
13 |
+
|
14 |
+
text = recognizer.recognize_whisper_api(
|
15 |
+
audio,
|
16 |
+
api_key=os.environ['OPENAI_API_KEY'],
|
17 |
+
)
|
18 |
+
|
19 |
+
return text
|
20 |
+
|
21 |
+
def speak(self, text):
|
22 |
+
audio = generate(
|
23 |
+
text=text,
|
24 |
+
voice='Bella',
|
25 |
+
model='eleven_monolingual_v1'
|
26 |
+
)
|
27 |
+
play(audio)
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai~=0.28.1
|
2 |
+
gradio~=3.46.0
|
3 |
+
python-dotenv~=1.0.0
|
4 |
+
langchain~=0.0.306
|
5 |
+
elevenlabs~=0.2.26
|