KASOTI_GAME / app.py
iisadia's picture
Update app.py
c4035be verified
raw
history blame
2.18 kB
import streamlit as st
import speech_recognition as sr
from langchain.llms import HuggingFaceHub
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# Initialize Hugging Face model (you can use other models based on your needs)
llm = HuggingFaceHub(
repo_id="gpt2", # You can replace with other models on Hugging Face
model_kwargs={"temperature": 0.7}
)
# Define the prompt template for translation or text generation
prompt_template = "Translate the following text to Urdu: {text}"
prompt = PromptTemplate(input_variables=["text"], template=prompt_template)
# Set up the chain
llm_chain = LLMChain(prompt=prompt, llm=llm)
# Function to process text using LangChain
def process_text(input_text):
result = llm_chain.run(input_text)
return result
# Function to recognize speech input
def recognize_speech():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
st.write("Please speak now...")
audio = recognizer.listen(source)
try:
text = recognizer.recognize_google(audio, language="en-US")
st.write(f"Recognized Text: {text}")
return text
except sr.UnknownValueError:
st.error("Sorry, I could not understand the audio.")
return ""
except sr.RequestError:
st.error("Could not request results from Google Speech Recognition service.")
return ""
# Streamlit UI
st.title("Voice and Text Input for English and Urdu")
st.write("You can either type your text or speak to input in English or Urdu!")
# Choose language input type
input_type = st.selectbox("Select Input Type", ["Text", "Voice"])
# Process based on input type
if input_type == "Text":
text_input = st.text_area("Enter text:")
if st.button("Submit"):
if text_input:
result = process_text(text_input)
st.write(f"Processed Result: {result}")
else:
st.error("Please enter some text.")
elif input_type == "Voice":
if st.button("Start Recording"):
speech_text = recognize_speech()
if speech_text:
result = process_text(speech_text)
st.write(f"Processed Result: {result}")