Spaces:
Runtime error
Runtime error
Commit
·
07b3297
1
Parent(s):
820c252
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from langchain.chat_models import ChatOpenAI
|
4 |
+
from langchain import LLMChain, PromptTemplate
|
5 |
+
from langchain.memory import ConversationBufferMemory
|
6 |
+
from gtts import gTTS
|
7 |
+
import io
|
8 |
+
import base64
|
9 |
+
import numpy as np
|
10 |
+
import tempfile
|
11 |
+
|
12 |
+
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
|
13 |
+
|
14 |
+
template = """ As a text generator, your main focus is to provide valuable information about mining acts, laws, and regulations specifically in India. Your responses should be restricted to queries regarding mining regulations and should avoid addressing unrelated topics such as general knowledge, full forms, celebrities, math problems, or programming. Ensure that your answers are accurate, concise, and helpful in guiding users through the complexities of mining legislation in India. Start by asking me any mining-related question you have, and I'll provide you with the information you need.
|
15 |
+
{chat_history}
|
16 |
+
User: {user_message}
|
17 |
+
Chatbot:"""
|
18 |
+
|
19 |
+
prompt = PromptTemplate(
|
20 |
+
input_variables=["chat_history", "user_message"], template=template
|
21 |
+
)
|
22 |
+
|
23 |
+
memory = ConversationBufferMemory(memory_key="chat_history")
|
24 |
+
|
25 |
+
llm_chain = LLMChain(
|
26 |
+
llm=ChatOpenAI(temperature='0.5', model_name="gpt-3.5-turbo"),
|
27 |
+
prompt=prompt,
|
28 |
+
verbose=True,
|
29 |
+
memory=memory,
|
30 |
+
)
|
31 |
+
|
32 |
+
def get_text_response(user_message, history):
|
33 |
+
response = llm_chain.predict(user_message=user_message)
|
34 |
+
return response
|
35 |
+
|
36 |
+
# Function to generate audio from text response
|
37 |
+
def get_audio_response(text_response):
|
38 |
+
try:
|
39 |
+
# Create a gTTS (Google Text-to-Speech) object from the text response
|
40 |
+
tts = gTTS(text=text_response, lang='en') # You can specify the language here
|
41 |
+
|
42 |
+
# Create a temporary file to save the audio
|
43 |
+
with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_audio_file:
|
44 |
+
tts.save(temp_audio_file.name)
|
45 |
+
|
46 |
+
# Return the temporary file path
|
47 |
+
return temp_audio_file.name
|
48 |
+
except Exception as e:
|
49 |
+
print(f"Error in get_audio_response: {str(e)}")
|
50 |
+
return str(e)
|
51 |
+
|
52 |
+
def get_combined_response(user_input, history):
|
53 |
+
text_response = get_text_response(user_input, history) # Assuming you have a 'history' variable
|
54 |
+
audio_response = get_audio_response(text_response) # Generate audio from the text response
|
55 |
+
|
56 |
+
return text_response, audio_response
|
57 |
+
|
58 |
+
input_interface = gr.Interface(
|
59 |
+
fn=get_combined_response,
|
60 |
+
inputs=gr.inputs.Textbox(label="User Input"),
|
61 |
+
outputs=[
|
62 |
+
gr.outputs.Textbox(label="Text Response"),
|
63 |
+
gr.outputs.Audio(label="Audio Response", type="numpy")
|
64 |
+
],
|
65 |
+
title="Text and Speech Chatbot",
|
66 |
+
description="Chatbot that provides text and speech responses.",
|
67 |
+
)
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
input_interface.launch()
|