Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,121 +1,10 @@
|
|
| 1 |
-
|
| 2 |
-
from transformers import pipeline
|
| 3 |
-
from gtts import gTTS
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
|
| 7 |
-
stt_pipeline = pipeline("automatic-speech-recognition", model="facebook/wav2vec2-base-960h")
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
filename = "response.mp3"
|
| 13 |
-
tts.save(filename)
|
| 14 |
-
return filename
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
if not audio_path:
|
| 19 |
-
return ""
|
| 20 |
-
text = stt_pipeline(audio_path)["text"]
|
| 21 |
-
return text.strip()
|
| 22 |
-
|
| 23 |
-
def process_interaction(audio, state, name, email):
|
| 24 |
-
"""Handle conversation flow and state management"""
|
| 25 |
-
if state == "get_name":
|
| 26 |
-
if audio:
|
| 27 |
-
name = transcribe_speech(audio)
|
| 28 |
-
if name:
|
| 29 |
-
# Confirm the name
|
| 30 |
-
prompt = f"Did you say your name is {name}? Please say 'Yes' or 'No'."
|
| 31 |
-
audio_response = text_to_speech(prompt)
|
| 32 |
-
return (
|
| 33 |
-
prompt,
|
| 34 |
-
"confirm_name",
|
| 35 |
-
name,
|
| 36 |
-
email,
|
| 37 |
-
audio_response
|
| 38 |
-
)
|
| 39 |
-
# Stay in name collection
|
| 40 |
-
prompt = "Welcome to Biryani Hub! Please tell me your name clearly."
|
| 41 |
-
return prompt, "get_name", name, email, text_to_speech(prompt)
|
| 42 |
-
|
| 43 |
-
elif state == "confirm_name":
|
| 44 |
-
if audio:
|
| 45 |
-
confirmation = transcribe_speech(audio).lower()
|
| 46 |
-
if "yes" in confirmation:
|
| 47 |
-
# Move to email collection
|
| 48 |
-
prompt = f"Hi {name}, please provide your email address."
|
| 49 |
-
audio_response = text_to_speech(prompt)
|
| 50 |
-
return (
|
| 51 |
-
prompt,
|
| 52 |
-
"get_email",
|
| 53 |
-
name,
|
| 54 |
-
email,
|
| 55 |
-
audio_response
|
| 56 |
-
)
|
| 57 |
-
elif "no" in confirmation:
|
| 58 |
-
# Retry name collection
|
| 59 |
-
prompt = "Sorry about that! Please tell me your name again."
|
| 60 |
-
return prompt, "get_name", "", email, text_to_speech(prompt)
|
| 61 |
-
# Stay in confirmation
|
| 62 |
-
prompt = f"Did you say your name is {name}? Please say 'Yes' or 'No'."
|
| 63 |
-
return prompt, "confirm_name", name, email, text_to_speech(prompt)
|
| 64 |
-
|
| 65 |
-
elif state == "get_email":
|
| 66 |
-
if audio:
|
| 67 |
-
email = transcribe_speech(audio)
|
| 68 |
-
if "@" in email and "." in email:
|
| 69 |
-
# Successful registration
|
| 70 |
-
prompt = f"Thank you, {name}! Your email {email} has been registered."
|
| 71 |
-
audio_response = text_to_speech(prompt)
|
| 72 |
-
return (
|
| 73 |
-
prompt,
|
| 74 |
-
"done",
|
| 75 |
-
name,
|
| 76 |
-
email,
|
| 77 |
-
audio_response
|
| 78 |
-
)
|
| 79 |
-
# Stay in email collection
|
| 80 |
-
prompt = "Please provide a valid email address."
|
| 81 |
-
return prompt, "get_email", name, email, text_to_speech(prompt)
|
| 82 |
-
|
| 83 |
-
# Initial state
|
| 84 |
-
prompt = "Welcome to Biryani Hub! Please tell me your name."
|
| 85 |
-
return prompt, "get_name", name, email, text_to_speech(prompt)
|
| 86 |
-
|
| 87 |
-
# Gradio interface setup
|
| 88 |
-
with gr.Blocks() as demo:
|
| 89 |
-
# State variables
|
| 90 |
-
state = gr.State("init")
|
| 91 |
-
name = gr.State("")
|
| 92 |
-
email = gr.State("")
|
| 93 |
-
|
| 94 |
-
with gr.Column():
|
| 95 |
-
# System responses
|
| 96 |
-
system_prompt = gr.Textbox(label="System Prompt", interactive=False)
|
| 97 |
-
system_audio = gr.Audio(label="Voice Response", autoplay=True)
|
| 98 |
-
|
| 99 |
-
# User inputs
|
| 100 |
-
with gr.Row():
|
| 101 |
-
name_field = gr.Textbox(label="Your Name", interactive=False)
|
| 102 |
-
email_field = gr.Textbox(label="Your Email", interactive=False)
|
| 103 |
-
user_audio = gr.Audio(type="filepath", label="Speak Here")
|
| 104 |
-
submit_btn = gr.Button("Submit")
|
| 105 |
-
|
| 106 |
-
# Initial greeting
|
| 107 |
-
demo.load(
|
| 108 |
-
process_interaction,
|
| 109 |
-
inputs=[None, state, name, email],
|
| 110 |
-
outputs=[system_prompt, state, name_field, email_field, system_audio]
|
| 111 |
-
)
|
| 112 |
-
|
| 113 |
-
# Interaction handling
|
| 114 |
-
submit_btn.click(
|
| 115 |
-
process_interaction,
|
| 116 |
-
inputs=[user_audio, state, name, email],
|
| 117 |
-
outputs=[system_prompt, state, name_field, email_field, system_audio]
|
| 118 |
-
)
|
| 119 |
-
|
| 120 |
-
# Launch the app
|
| 121 |
-
demo.launch()
|
|
|
|
| 1 |
+
from flask import Flask, render_template
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
app = Flask(__name__)
|
|
|
|
| 4 |
|
| 5 |
+
@app.route("/")
|
| 6 |
+
def home():
|
| 7 |
+
return render_template("index.html")
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
if __name__ == "__main__":
|
| 10 |
+
app.run(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|