Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,104 +2,54 @@ import streamlit as st
|
|
2 |
from audio_recorder_streamlit import audio_recorder
|
3 |
import speech_recognition as sr
|
4 |
from gtts import gTTS
|
5 |
-
import os
|
6 |
import tempfile
|
7 |
-
|
8 |
-
from
|
9 |
-
from langchain.vectorstores import FAISS
|
10 |
-
from langchain.embeddings.openai import OpenAIEmbeddings
|
11 |
-
|
12 |
-
# Claude (Anthropic) API setup for Langchain
|
13 |
-
CLAUDE_API_KEY = st.secrets['claude']['api_key']
|
14 |
-
llm = Anthropic(model="claude-v1", api_key=CLAUDE_API_KEY)
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
# Main function
|
21 |
def main():
|
22 |
-
st.
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
.audio-recorder-container {
|
34 |
-
padding: 20px;
|
35 |
-
border: 2px solid #ccc;
|
36 |
-
border-radius: 10px;
|
37 |
-
background-color: #f9f9f9;
|
38 |
-
display: flex;
|
39 |
-
justify-content: center;
|
40 |
-
align-items: center;
|
41 |
-
position: relative;
|
42 |
-
}
|
43 |
-
</style>
|
44 |
-
""",
|
45 |
-
unsafe_allow_html=True
|
46 |
-
)
|
47 |
-
|
48 |
-
st.markdown("<h1 class='header-text'>🎤 اردو وائس چیٹ بوٹ</h1>", unsafe_allow_html=True)
|
49 |
-
st.subheader('اپنی آواز ریکارڈ کریں اور اردو جواب سنیں', divider='rainbow')
|
50 |
-
|
51 |
-
st.sidebar.image('Hamesh_Raj_Profile_Photo.png', use_column_width=True)
|
52 |
-
st.sidebar.header("**انجینئر ہمیش راج**")
|
53 |
-
st.sidebar.write("جنریٹیو اے آئی اور مشین لرننگ میں دو سال کا تجربہ")
|
54 |
-
|
55 |
-
st.sidebar.header("چیٹ بوٹ کی معلومات", divider='rainbow')
|
56 |
-
st.sidebar.write('یہ چیٹ بوٹ اردو زبان میں صوتی پیغامات وصول کرتا ہے اور صوتی پیغامات میں ہی جواب دیتا ہے۔')
|
57 |
-
|
58 |
-
st.sidebar.header("رابطہ کی معلومات", divider='rainbow')
|
59 |
-
st.sidebar.write("[LinkedIn](https://www.linkedin.com/in/datascientisthameshraj/)")
|
60 |
-
st.sidebar.write("[GitHub](https://github.com/mldatascientist23)")
|
61 |
-
st.sidebar.write("[Email]([email protected])")
|
62 |
-
|
63 |
-
with st.container():
|
64 |
-
st.markdown('<div class="audio-recorder-container">', unsafe_allow_html=True)
|
65 |
-
# Audio recorder for Urdu input
|
66 |
-
audio_data = audio_recorder(text='بولیۓ', icon_size="2x", icon_name="microphone-lines", key="urdu_recorder")
|
67 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
68 |
-
|
69 |
if audio_data is not None:
|
70 |
-
|
71 |
-
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
st.markdown('<h2 class="avatar">🧑</h2>', unsafe_allow_html=True)
|
76 |
-
st.audio(audio_data)
|
77 |
-
|
78 |
-
# Save the recorded audio to a temporary file
|
79 |
-
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file:
|
80 |
-
temp_audio_file.write(audio_data)
|
81 |
-
temp_audio_file_path = temp_audio_file.name
|
82 |
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
86 |
|
87 |
-
|
88 |
-
|
89 |
|
90 |
-
#
|
91 |
-
|
|
|
92 |
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
with col1:
|
97 |
-
# Convert the response text to speech
|
98 |
-
convert_text_to_audio(response_text)
|
99 |
-
st.markdown(f'<div class="bot-text">{response_text}</div>', unsafe_allow_html=True)
|
100 |
|
|
|
101 |
def convert_audio_to_text(audio_file_path):
|
102 |
-
# Convert Urdu audio to text using speech recognition
|
103 |
recognizer = sr.Recognizer()
|
104 |
with sr.AudioFile(audio_file_path) as source:
|
105 |
audio_data = recognizer.record(source)
|
@@ -107,26 +57,33 @@ def convert_audio_to_text(audio_file_path):
|
|
107 |
text = recognizer.recognize_google(audio_data, language="ur")
|
108 |
return text
|
109 |
except sr.UnknownValueError:
|
110 |
-
return "آپ کی آواز
|
111 |
except sr.RequestError:
|
112 |
-
return "معذرت،
|
113 |
|
114 |
-
|
|
|
|
|
115 |
try:
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
|
|
|
|
|
|
120 |
except Exception as e:
|
121 |
-
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
|
|
|
|
130 |
|
131 |
if __name__ == "__main__":
|
132 |
main()
|
|
|
2 |
from audio_recorder_streamlit import audio_recorder
|
3 |
import speech_recognition as sr
|
4 |
from gtts import gTTS
|
|
|
5 |
import tempfile
|
6 |
+
import os
|
7 |
+
from anthropic import Client # For Claude Haiku model
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
# Claude API setup
|
10 |
+
CLAUDE_API_KEY = st.secrets['claude_api_key'] # Store your Claude API key in Streamlit secrets
|
11 |
+
client = Client(api_key=CLAUDE_API_KEY)
|
12 |
|
13 |
+
# Main function for chatbot app
|
14 |
def main():
|
15 |
+
st.title("🎤 اردو وائس چیٹ بوٹ")
|
16 |
+
|
17 |
+
# Sidebar with information
|
18 |
+
st.sidebar.title("حامش راج")
|
19 |
+
st.sidebar.write("ماہر ڈیٹا سائنس اور جنریٹو اے آئی")
|
20 |
+
|
21 |
+
st.markdown("**اپنی آواز ریکارڈ کریں اور جواب حاصل کریں**")
|
22 |
+
|
23 |
+
# Audio Recorder
|
24 |
+
audio_data = audio_recorder(text='اردو میں بولیئے', icon_size="2x", icon_name="microphone-lines", key="urdu_recorder")
|
25 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
if audio_data is not None:
|
27 |
+
# Save the recorded audio to a temporary file
|
28 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_audio_file:
|
29 |
+
temp_audio_file.write(audio_data)
|
30 |
+
temp_audio_file_path = temp_audio_file.name
|
31 |
|
32 |
+
# Convert audio to text (Speech to Text in Urdu)
|
33 |
+
user_input_text = convert_audio_to_text(temp_audio_file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
# Display user input text
|
36 |
+
st.write(f"**آپ نے کہا:** {user_input_text}")
|
37 |
+
|
38 |
+
# Get LLM (Claude) response
|
39 |
+
response_text = get_claude_response(user_input_text)
|
40 |
|
41 |
+
# Display chatbot's text response
|
42 |
+
st.write(f"**جواب:** {response_text}")
|
43 |
|
44 |
+
# Convert response text to audio and play it
|
45 |
+
response_audio = convert_text_to_audio(response_text)
|
46 |
+
st.audio(response_audio)
|
47 |
|
48 |
+
# Clean up temporary audio file
|
49 |
+
os.remove(temp_audio_file_path)
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
+
# Function to convert audio to text (Urdu Speech Recognition)
|
52 |
def convert_audio_to_text(audio_file_path):
|
|
|
53 |
recognizer = sr.Recognizer()
|
54 |
with sr.AudioFile(audio_file_path) as source:
|
55 |
audio_data = recognizer.record(source)
|
|
|
57 |
text = recognizer.recognize_google(audio_data, language="ur")
|
58 |
return text
|
59 |
except sr.UnknownValueError:
|
60 |
+
return "معذرت، میں آپ کی آواز سمجھ نہیں سکا"
|
61 |
except sr.RequestError:
|
62 |
+
return "معذرت، سرور دستیاب نہیں ہے"
|
63 |
|
64 |
+
# Function to get response from Claude (Langchain with RAG)
|
65 |
+
def get_claude_response(prompt_text):
|
66 |
+
prompt = f"براہ کرم اردو میں جواب دیں: {prompt_text}"
|
67 |
try:
|
68 |
+
# Query Claude Haiku via Langchain
|
69 |
+
response = client.completions.create(
|
70 |
+
model="claude-v1", # Claude Haiku model
|
71 |
+
prompt=prompt,
|
72 |
+
max_tokens_to_sample=100,
|
73 |
+
)
|
74 |
+
return response['completion']
|
75 |
except Exception as e:
|
76 |
+
return f"خطا: {str(e)}"
|
77 |
|
78 |
+
# Function to convert text to Urdu audio (Text-to-Speech)
|
79 |
+
def convert_text_to_audio(text):
|
80 |
+
try:
|
81 |
+
tts = gTTS(text=text, lang='ur')
|
82 |
+
temp_audio_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False).name
|
83 |
+
tts.save(temp_audio_path)
|
84 |
+
return temp_audio_path
|
85 |
+
except Exception as e:
|
86 |
+
return None
|
87 |
|
88 |
if __name__ == "__main__":
|
89 |
main()
|