Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,67 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import datetime
|
| 6 |
-
import wikipedia #pip install wikipedia
|
| 7 |
-
import webbrowser
|
| 8 |
-
import os
|
| 9 |
-
import smtplib
|
| 10 |
|
| 11 |
-
|
| 12 |
voices = engine.getProperty('voices')
|
| 13 |
-
|
| 14 |
-
engine.setProperty('voice', voices[0].id)
|
| 15 |
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
engine.runAndWait()
|
| 20 |
|
| 21 |
-
|
| 22 |
-
def
|
| 23 |
-
hour = int(datetime.datetime.now().hour)
|
| 24 |
-
if hour>=0 and hour<12:
|
| 25 |
-
speak("Good Morning!")
|
| 26 |
-
|
| 27 |
-
elif hour>=12 and hour<18:
|
| 28 |
-
speak("Good Afternoon!")
|
| 29 |
-
|
| 30 |
-
else:
|
| 31 |
-
speak("Good Evening!")
|
| 32 |
-
|
| 33 |
-
speak("I am Jarvis Sir. Please tell me how may I help you")
|
| 34 |
-
|
| 35 |
-
def takeCommand():
|
| 36 |
-
#It takes microphone input from the user and returns string output
|
| 37 |
-
|
| 38 |
-
r = sr.Recognizer()
|
| 39 |
with sr.Microphone() as source:
|
| 40 |
-
print("
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
| 65 |
-
while True:
|
| 66 |
-
# if 1:
|
| 67 |
-
query = takeCommand().lower()
|
| 68 |
-
|
| 69 |
-
# Logic for executing tasks based on query
|
| 70 |
-
if 'wikipedia' in query:
|
| 71 |
-
speak('Searching Wikipedia...')
|
| 72 |
-
query = query.replace("wikipedia", "")
|
| 73 |
-
results = wikipedia.summary(query, sentences=2)
|
| 74 |
-
speak("According to Wikipedia")
|
| 75 |
-
print(results)
|
| 76 |
-
speak(results)
|
| 77 |
-
|
| 78 |
-
elif 'open youtube' in query:
|
| 79 |
-
webbrowser.open("youtube.com")
|
| 80 |
-
|
| 81 |
-
elif 'open google' in query:
|
| 82 |
-
webbrowser.open("google.com")
|
| 83 |
-
|
| 84 |
-
elif 'open stackoverflow' in query:
|
| 85 |
-
webbrowser.open("stackoverflow.com")
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
elif 'play music' in query:
|
| 89 |
-
music_dir = 'D:\\Non Critical\\songs\\Favorite Songs2'
|
| 90 |
-
songs = os.listdir(music_dir)
|
| 91 |
-
print(songs)
|
| 92 |
-
os.startfile(os.path.join(music_dir, songs[0]))
|
| 93 |
-
|
| 94 |
-
elif 'the time' in query:
|
| 95 |
-
strTime = datetime.datetime.now().strftime("%H:%M:%S")
|
| 96 |
-
speak(f"Sir, the time is {strTime}")
|
| 97 |
-
|
| 98 |
-
elif 'open code' in query:
|
| 99 |
-
codePath = "C:\\Users\\Haris\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
|
| 100 |
-
os.startfile(codePath)
|
| 101 |
-
|
| 102 |
-
elif 'email to harry' in query:
|
| 103 |
-
try:
|
| 104 |
-
speak("What should I say?")
|
| 105 |
-
content = takeCommand()
|
| 106 |
-
to = "harryyourEmail@gmail.com"
|
| 107 |
-
sendEmail(to, content)
|
| 108 |
-
speak("Email has been sent!")
|
| 109 |
-
except Exception as e:
|
| 110 |
-
print(e)
|
| 111 |
-
speak("Sorry my friend harry bhai. I am not able to send this email")
|
| 112 |
-
else:
|
| 113 |
-
print("No query matched")
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pyttsx3
|
| 3 |
+
import speech_recognition as sr
|
| 4 |
+
from transformers import pipeline
|
| 5 |
|
| 6 |
+
# Initialize Text-to-Speech engine
|
| 7 |
+
engine = pyttsx3.init()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Set the voice to a friendly one (you can change it as per your preference)
|
| 10 |
voices = engine.getProperty('voices')
|
| 11 |
+
engine.setProperty('voice', voices[1].id) # Female voice, you can change to male
|
|
|
|
| 12 |
|
| 13 |
+
# Initialize Speech-to-Text model from Hugging Face (you can customize this)
|
| 14 |
+
recognizer = sr.Recognizer()
|
| 15 |
|
| 16 |
+
# Function to convert text to speech
|
| 17 |
+
def speak_response(text):
|
| 18 |
+
engine.say(text)
|
| 19 |
engine.runAndWait()
|
| 20 |
|
| 21 |
+
# Function to process the speech input (convert to text)
|
| 22 |
+
def listen_input():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
with sr.Microphone() as source:
|
| 24 |
+
print("Say something...")
|
| 25 |
+
audio = recognizer.listen(source)
|
| 26 |
+
try:
|
| 27 |
+
command = recognizer.recognize_google(audio)
|
| 28 |
+
print("You said: " + command)
|
| 29 |
+
return command
|
| 30 |
+
except sr.UnknownValueError:
|
| 31 |
+
return "Sorry, I didn't understand that."
|
| 32 |
+
except sr.RequestError:
|
| 33 |
+
return "Sorry, I'm having trouble with the service."
|
| 34 |
+
|
| 35 |
+
# Function to handle menu items and return response
|
| 36 |
+
def handle_order(item):
|
| 37 |
+
if item.lower() == "pizza":
|
| 38 |
+
response = "You have selected Pizza. It contains cheese, tomato sauce, and pepperoni."
|
| 39 |
+
elif item.lower() == "burger":
|
| 40 |
+
response = "You have selected Burger. It contains beef patty, lettuce, and cheese."
|
| 41 |
+
else:
|
| 42 |
+
response = "Sorry, I don't have information about that item."
|
| 43 |
+
speak_response(response)
|
| 44 |
+
return response
|
| 45 |
+
|
| 46 |
+
# Gradio Interface setup
|
| 47 |
+
def chatbot_interface():
|
| 48 |
+
with gr.Blocks() as demo:
|
| 49 |
+
with gr.Row():
|
| 50 |
+
gr.Image("assets/logo.png", label="Click to speak", elem_id="voice_logo", interactive=True)
|
| 51 |
+
|
| 52 |
+
text_input = gr.Textbox(label="Order item", placeholder="Say or type your order", lines=1)
|
| 53 |
+
output_text = gr.Textbox(label="Assistant Response", interactive=False)
|
| 54 |
+
|
| 55 |
+
def on_voice_logo_click():
|
| 56 |
+
user_input = listen_input()
|
| 57 |
+
response = handle_order(user_input)
|
| 58 |
+
output_text.update(value=response)
|
| 59 |
+
return response
|
| 60 |
+
|
| 61 |
+
# Attach voice logo action
|
| 62 |
+
gr.Button("Speak").click(on_voice_logo_click)
|
| 63 |
+
|
| 64 |
+
demo.launch()
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
+
chatbot_interface()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|