Spaces:
Sleeping
Sleeping
initial commit
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from dotenv import load_dotenv
|
3 |
+
from gradio import ChatMessage
|
4 |
+
from setup import Speech_Text
|
5 |
+
from temp import Script
|
6 |
+
load_dotenv()
|
7 |
+
|
8 |
+
transcriptor = Speech_Text()
|
9 |
+
output_id = None
|
10 |
+
database_con = Script()
|
11 |
+
|
12 |
+
# Function to generate chatbot response
|
13 |
+
def generate_response(chat_history: list[ChatMessage]):
|
14 |
+
return database_con.request(chat_history)
|
15 |
+
|
16 |
+
def process(audio, input_text, chat_history: list[ChatMessage]):
|
17 |
+
|
18 |
+
if audio is not None:
|
19 |
+
transcript = transcriptor.get_transcript(audio)
|
20 |
+
chat_history.append({"role": "user", "content": transcript})
|
21 |
+
|
22 |
+
elif input_text:
|
23 |
+
chat_history.append({"role": "user", "content": input_text})
|
24 |
+
|
25 |
+
else:
|
26 |
+
response = 'Provide a query text or an audio to query.'
|
27 |
+
chat_history.append({"role": "assistant", "content": response})
|
28 |
+
audio_data = transcriptor.speech_synthesis(response)
|
29 |
+
return audio_data, chat_history
|
30 |
+
|
31 |
+
response = generate_response(chat_history)
|
32 |
+
chat_history.append({"role": "assistant", "content": response})
|
33 |
+
# audio_data = transcriptor.speech_synthesis(response)
|
34 |
+
return chat_history
|
35 |
+
|
36 |
+
# Create Gradio Blocks interface
|
37 |
+
with gr.Blocks() as demo:
|
38 |
+
|
39 |
+
with gr.Row():
|
40 |
+
chatbot = gr.Chatbot(label="Chatbot Conversation", type="messages", bubble_full_width=True, show_copy_button=True, autoscroll=True)
|
41 |
+
|
42 |
+
with gr.Row():
|
43 |
+
input_textbox = gr.Textbox(label="Input Text", placeholder="Type your message here...")
|
44 |
+
input_audio = gr.Audio(label="Input Audio", sources="microphone", type="numpy")
|
45 |
+
|
46 |
+
process_button = gr.Button("Submit Query")
|
47 |
+
|
48 |
+
process_button.click(
|
49 |
+
fn=process,
|
50 |
+
inputs=[input_audio, input_textbox, chatbot],
|
51 |
+
outputs=[chatbot]
|
52 |
+
)
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
demo.launch(server_name="0.0.0.0",server_port=80)
|
requirements.txt
ADDED
Binary file (4.01 kB). View file
|
|
setup.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
import io
|
4 |
+
from groq import Groq
|
5 |
+
import soundfile as sf
|
6 |
+
from deepgram import DeepgramClient, SpeakOptions
|
7 |
+
from langchain_groq import ChatGroq
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
load_dotenv('.env')
|
11 |
+
|
12 |
+
|
13 |
+
# Text to Speech and Speech to Text
|
14 |
+
class Speech_Text():
|
15 |
+
def __init__(self):
|
16 |
+
self.client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
17 |
+
|
18 |
+
# Function to get transcript from audio
|
19 |
+
def get_transcript(self,audio):
|
20 |
+
audio_buffer = io.BytesIO()
|
21 |
+
sf.write(audio_buffer, audio[1], samplerate=audio[0], format="MP3")
|
22 |
+
audio_buffer.seek(0)
|
23 |
+
translation = self.client.audio.transcriptions.create(
|
24 |
+
file=("audio.mp3", audio_buffer.read()),
|
25 |
+
model="distil-whisper-large-v3-en",
|
26 |
+
response_format="json",
|
27 |
+
temperature=0.0,
|
28 |
+
)
|
29 |
+
return translation.text
|
30 |
+
|
31 |
+
|
temp.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_community.utilities import SerpAPIWrapper
|
2 |
+
from langgraph.prebuilt import create_react_agent
|
3 |
+
import getpass
|
4 |
+
import os
|
5 |
+
import sqlite3
|
6 |
+
from langchain_openai import ChatOpenAI
|
7 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
8 |
+
from langchain_core.messages import HumanMessage,AIMessage
|
9 |
+
from langchain.agents import initialize_agent
|
10 |
+
from langchain.agents.agent_types import AgentType
|
11 |
+
from langchain_community.agent_toolkits.sql.toolkit import SQLDatabaseToolkit
|
12 |
+
from langchain_community.utilities.sql_database import SQLDatabase
|
13 |
+
from langchain_openai import ChatOpenAI
|
14 |
+
from langchain_groq import ChatGroq
|
15 |
+
|
16 |
+
from dotenv import load_dotenv
|
17 |
+
load_dotenv('.env')
|
18 |
+
|
19 |
+
class Script:
|
20 |
+
def __init__(self):
|
21 |
+
self.model = ChatOpenAI(model="gpt-4o-mini")
|
22 |
+
self.groq = ChatGroq(model='llama3-70b-8192')
|
23 |
+
self.db = SQLDatabase.from_uri("sqlite:///sample_database.db")
|
24 |
+
self.toolkit = SQLDatabaseToolkit(db=self.db, llm=self.model)
|
25 |
+
search = TavilySearchResults(max_results=2)
|
26 |
+
tools = self.toolkit.get_tools()+[search]
|
27 |
+
self.agent = create_react_agent(self.model, tools)
|
28 |
+
|
29 |
+
def history(self,hist):
|
30 |
+
message = []
|
31 |
+
for i in hist[-5:]:
|
32 |
+
if(i['role']=='user'):
|
33 |
+
message+=[HumanMessage(content=i['content'])]
|
34 |
+
else:
|
35 |
+
message+=[AIMessage(content=i['content'])]
|
36 |
+
return message
|
37 |
+
|
38 |
+
def request(self,message):
|
39 |
+
message = self.history(message)
|
40 |
+
response = self.agent.invoke({"messages": message})
|
41 |
+
return response["messages"][-1].content
|