datascientist22 commited on
Commit
f7edc61
1 Parent(s): 026325d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -101
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
- from langchain.chains import RetrievalQA
8
- from langchain.llms import Anthropic # Claude's API from Langchain
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
- # Setup the vector store for RAG
17
- embedding_model = OpenAIEmbeddings()
18
- vector_store = FAISS.load_local("my_vector_store", embedding_model)
19
 
20
- # Main function
21
  def main():
22
- st.markdown(
23
- """
24
- <style>
25
- .stApp {
26
- background: linear-gradient(90deg, rgba(36,0,2,1) 0%, rgba(121,9,9,1) 50%, rgba(255,212,0,1) 100%);
27
- }
28
- .header-text {
29
- color: white;
30
- text-shadow: 2px 2px 4px #000000;
31
- text-align: center;
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
- with st.container():
71
- col1, col2 = st.columns(2)
 
 
72
 
73
- with col2:
74
- # Display the recorded audio file
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
- # Convert audio file to text
84
- text = convert_audio_to_text(temp_audio_file_path)
85
- st.markdown(f'<div class="user-text">{text}</div>', unsafe_allow_html=True)
 
 
86
 
87
- # Remove the temporary file
88
- os.remove(temp_audio_file_path)
89
 
90
- # Get response from the Langchain RAG model
91
- response_text = get_llm_response(text)
 
92
 
93
- with st.container():
94
- col1, col2 = st.columns(2)
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
- def convert_text_to_audio(text, lang='ur'):
 
 
115
  try:
116
- tts = gTTS(text=text, lang=lang)
117
- tts_audio_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False).name
118
- tts.save(tts_audio_path)
119
- st.audio(tts_audio_path, format='audio/mp3')
 
 
 
120
  except Exception as e:
121
- st.error(f"آواز میں تبدیل کرنے میں خرابی: {e}")
122
 
123
- def get_llm_response(text):
124
- # Create the retrieval-augmented generation chain
125
- qa_chain = RetrievalQA(llm=llm, retriever=vector_store.as_retriever())
126
-
127
- # Generate the response
128
- response = qa_chain.run(text)
129
- return response
 
 
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()