Spaces:
Sleeping
Sleeping
datascientist22
commited on
Commit
•
75a1291
1
Parent(s):
be92c14
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import speech_recognition as sr
|
3 |
+
from gtts import gTTS
|
4 |
+
import requests
|
5 |
+
import os
|
6 |
+
import tempfile
|
7 |
+
from langchain.chains import ConversationalRetrievalChain
|
8 |
+
from langchain.vectorstores import FAISS
|
9 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
10 |
+
from langchain.text_splitter import CharacterTextSplitter
|
11 |
+
from langchain.docstore.document import Document
|
12 |
+
from langchain.prompts import PromptTemplate
|
13 |
+
from langchain.llms import Anthropic
|
14 |
+
from langchain.llms import AI21 # Use Claude or AI21 API
|
15 |
+
|
16 |
+
# LLM API settings (choose between Claude or AI21)
|
17 |
+
CLAUDE_API_KEY = st.secrets["claude_api_key"]
|
18 |
+
# Initialize language model and embeddings
|
19 |
+
llm = Anthropic(api_key=CLAUDE_API_KEY) # Or AI21 for the API
|
20 |
+
|
21 |
+
# Function for speech-to-text (Audio to Text)
|
22 |
+
def record_audio():
|
23 |
+
recognizer = sr.Recognizer()
|
24 |
+
with sr.Microphone() as source:
|
25 |
+
st.info("Listening... Please speak now.")
|
26 |
+
audio = recognizer.listen(source)
|
27 |
+
try:
|
28 |
+
text_urdu = recognizer.recognize_google(audio, language="ur")
|
29 |
+
return text_urdu
|
30 |
+
except sr.UnknownValueError:
|
31 |
+
st.error("Sorry, I could not understand the audio.")
|
32 |
+
return None
|
33 |
+
|
34 |
+
# Function for text-to-speech (Text to Audio)
|
35 |
+
def text_to_speech_urdu(text):
|
36 |
+
tts = gTTS(text=text, lang='ur')
|
37 |
+
tts.save("response.mp3")
|
38 |
+
with open("response.mp3", "rb") as audio_file:
|
39 |
+
audio_bytes = audio_file.read()
|
40 |
+
st.audio(audio_bytes, format="audio/mp3")
|
41 |
+
if os.path.exists("response.mp3"):
|
42 |
+
os.remove("response.mp3")
|
43 |
+
|
44 |
+
# Function to handle file uploads (process text content)
|
45 |
+
def process_file_upload(file):
|
46 |
+
if file.type == "text/plain":
|
47 |
+
text_content = file.read().decode("utf-8")
|
48 |
+
elif file.type == "application/pdf":
|
49 |
+
# Process PDF files
|
50 |
+
text_content = extract_text_from_pdf(file)
|
51 |
+
else:
|
52 |
+
st.warning("Unsupported file format. Please upload a text or PDF file.")
|
53 |
+
return None
|
54 |
+
return text_content
|
55 |
+
|
56 |
+
def extract_text_from_pdf(file):
|
57 |
+
# Extract text from uploaded PDF file (you can use PyPDF2 or other libraries)
|
58 |
+
pass
|
59 |
+
|
60 |
+
# Function to create a Langchain Conversational Retrieval Chain (RAG)
|
61 |
+
def create_chain(docs):
|
62 |
+
embeddings = HuggingFaceEmbeddings()
|
63 |
+
vectorstore = FAISS.from_documents(docs, embeddings)
|
64 |
+
chain = ConversationalRetrievalChain.from_llm(
|
65 |
+
llm=llm,
|
66 |
+
retriever=vectorstore.as_retriever(),
|
67 |
+
)
|
68 |
+
return chain
|
69 |
+
|
70 |
+
# Streamlit app layout
|
71 |
+
st.title("🤖 Urdu Voice & File Chatbot using Langchain (RAG)")
|
72 |
+
st.write("Interact with the chatbot using Urdu voice input or upload any file in any language. The chatbot will respond with both text and audio in Urdu.")
|
73 |
+
|
74 |
+
# Upload a file
|
75 |
+
uploaded_file = st.file_uploader("Upload a file (text, PDF)", type=["txt", "pdf"])
|
76 |
+
|
77 |
+
# Record and process audio input
|
78 |
+
if st.button("Record Urdu Query"):
|
79 |
+
user_input_urdu = record_audio()
|
80 |
+
if user_input_urdu:
|
81 |
+
st.write(f"Your Query (Text): {user_input_urdu}")
|
82 |
+
|
83 |
+
# Simulate document processing with RAG
|
84 |
+
docs = [Document(page_content=user_input_urdu)]
|
85 |
+
chain = create_chain(docs)
|
86 |
+
response = chain.run(user_input_urdu)
|
87 |
+
|
88 |
+
st.write(f"Chatbot Response (Text): {response}")
|
89 |
+
text_to_speech_urdu(response)
|
90 |
+
else:
|
91 |
+
st.error("No valid input provided. Please try again.")
|
92 |
+
|
93 |
+
# Process uploaded file if any
|
94 |
+
if uploaded_file:
|
95 |
+
file_text = process_file_upload(uploaded_file)
|
96 |
+
if file_text:
|
97 |
+
st.write(f"File Content (Text): {file_text}")
|
98 |
+
|
99 |
+
# Generate response based on file content
|
100 |
+
docs = [Document(page_content=file_text)]
|
101 |
+
chain = create_chain(docs)
|
102 |
+
response = chain.run(file_text)
|
103 |
+
|
104 |
+
st.write(f"Chatbot Response (Text): {response}")
|
105 |
+
text_to_speech_urdu(response)
|