Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,63 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
"text-generation",
|
12 |
-
model=model_name,
|
13 |
-
tokenizer=tokenizer,
|
14 |
-
torch_dtype=torch.float16,
|
15 |
-
device_map="auto" # Uses GPU if available
|
16 |
)
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
def
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import speech_recognition as sr
|
3 |
+
from langchain.llms import HuggingFaceHub
|
4 |
+
from langchain.prompts import PromptTemplate
|
5 |
+
from langchain.chains import LLMChain
|
6 |
+
|
7 |
+
# Initialize Hugging Face model (you can use other models based on your needs)
|
8 |
+
llm = HuggingFaceHub(
|
9 |
+
repo_id="gpt2", # You can replace with other models on Hugging Face
|
10 |
+
model_kwargs={"temperature": 0.7}
|
|
|
|
|
|
|
|
|
|
|
11 |
)
|
12 |
|
13 |
+
# Define the prompt template for translation or text generation
|
14 |
+
prompt_template = "Translate the following text to Urdu: {text}"
|
15 |
+
prompt = PromptTemplate(input_variables=["text"], template=prompt_template)
|
16 |
+
|
17 |
+
# Set up the chain
|
18 |
+
llm_chain = LLMChain(prompt=prompt, llm=llm)
|
19 |
+
|
20 |
+
# Function to process text using LangChain
|
21 |
+
def process_text(input_text):
|
22 |
+
result = llm_chain.run(input_text)
|
23 |
+
return result
|
24 |
+
|
25 |
+
# Function to recognize speech input
|
26 |
+
def recognize_speech():
|
27 |
+
recognizer = sr.Recognizer()
|
28 |
+
with sr.Microphone() as source:
|
29 |
+
st.write("Please speak now...")
|
30 |
+
audio = recognizer.listen(source)
|
31 |
+
try:
|
32 |
+
text = recognizer.recognize_google(audio, language="en-US")
|
33 |
+
st.write(f"Recognized Text: {text}")
|
34 |
+
return text
|
35 |
+
except sr.UnknownValueError:
|
36 |
+
st.error("Sorry, I could not understand the audio.")
|
37 |
+
return ""
|
38 |
+
except sr.RequestError:
|
39 |
+
st.error("Could not request results from Google Speech Recognition service.")
|
40 |
+
return ""
|
41 |
+
|
42 |
+
# Streamlit UI
|
43 |
+
st.title("Voice and Text Input for English and Urdu")
|
44 |
+
st.write("You can either type your text or speak to input in English or Urdu!")
|
45 |
+
|
46 |
+
# Choose language input type
|
47 |
+
input_type = st.selectbox("Select Input Type", ["Text", "Voice"])
|
48 |
+
|
49 |
+
# Process based on input type
|
50 |
+
if input_type == "Text":
|
51 |
+
text_input = st.text_area("Enter text:")
|
52 |
+
if st.button("Submit"):
|
53 |
+
if text_input:
|
54 |
+
result = process_text(text_input)
|
55 |
+
st.write(f"Processed Result: {result}")
|
56 |
+
else:
|
57 |
+
st.error("Please enter some text.")
|
58 |
+
elif input_type == "Voice":
|
59 |
+
if st.button("Start Recording"):
|
60 |
+
speech_text = recognize_speech()
|
61 |
+
if speech_text:
|
62 |
+
result = process_text(speech_text)
|
63 |
+
st.write(f"Processed Result: {result}")
|