iisadia commited on
Commit
c4035be
·
verified ·
1 Parent(s): f218dcf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -41
app.py CHANGED
@@ -1,43 +1,63 @@
1
- from flask import Flask, request, render_template
2
- from transformers import pipeline, AutoTokenizer
3
- import torch
4
-
5
- app = Flask(__name__)
6
-
7
- # Load a lightweight model (e.g., Zephyr-7B, Mistral-7B)
8
- model_name = "mistralai/Mistral-7B-Instruct-v0.2"
9
- tokenizer = AutoTokenizer.from_pretrained(model_name)
10
- chatbot = pipeline(
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
- @app.route("/", methods=["GET", "POST"])
19
- def home():
20
- if request.method == "POST":
21
- user_input = request.form["user_input"]
22
- response = generate_response(user_input)
23
- return render_template("index.html", user_input=user_input, bot_response=response)
24
- return render_template("index.html")
25
-
26
- def generate_response(prompt):
27
- # Format prompt for instruction-following models
28
- messages = [{"role": "user", "content": prompt}]
29
- prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
30
-
31
- # Generate response
32
- outputs = chatbot(
33
- prompt,
34
- max_new_tokens=256,
35
- do_sample=True,
36
- temperature=0.7,
37
- top_k=50,
38
- top_p=0.95
39
- )
40
- return outputs[0]["generated_text"][len(prompt):] # Extract only the bot's reply
41
-
42
- if __name__ == "__main__":
43
- app.run(host="0.0.0.0", port=5000, debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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}")