File size: 2,183 Bytes
c4035be
 
 
 
 
 
 
 
 
 
e1bf4d4
 
c4035be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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}")