Spaces:
Build error
Build error
| from gtts import gTTS | |
| import wikipedia | |
| import datetime | |
| import os | |
| import gradio as gr | |
| # Text-to-Speech using gTTS | |
| def speak(audio): | |
| """Generate speech audio from text.""" | |
| tts = gTTS(text=audio, lang="en") | |
| tts.save("response.mp3") | |
| return "response.mp3" # Return the audio file path | |
| # Generate greeting based on time | |
| def wishMe(): | |
| hour = int(datetime.datetime.now().hour) | |
| if hour >= 0 and hour < 12: | |
| return "Good Morning! I am Jarvis. How may I assist you?" | |
| elif hour >= 12 and hour < 18: | |
| return "Good Afternoon! I am Jarvis. How may I assist you?" | |
| else: | |
| return "Good Evening! I am Jarvis. How may I assist you?" | |
| # Process user query | |
| def process_query(query): | |
| query = query.lower() | |
| response = "" | |
| if 'wikipedia' in query: | |
| query = query.replace("wikipedia", "") | |
| try: | |
| results = wikipedia.summary(query, sentences=2) | |
| response = f"According to Wikipedia: {results}" | |
| except Exception as e: | |
| response = "I couldn't find anything on Wikipedia." | |
| elif 'time' in query: | |
| current_time = datetime.datetime.now().strftime("%H:%M:%S") | |
| response = f"The current time is {current_time}." | |
| else: | |
| response = "I am not sure how to respond to that. Please try again." | |
| return response | |
| # Combine greeting and response | |
| def jarvis_assistant(audio_input): | |
| try: | |
| # Process voice input | |
| import speech_recognition as sr | |
| recognizer = sr.Recognizer() | |
| with sr.AudioFile(audio_input) as source: | |
| audio_data = recognizer.record(source) | |
| query = recognizer.recognize_google(audio_data) | |
| # Generate greeting and response | |
| greeting = wishMe() | |
| response = process_query(query) | |
| final_response = f"{greeting}\n\n{response}" | |
| # Convert the response to speech | |
| audio_output = speak(final_response) | |
| return final_response, audio_output | |
| except Exception as e: | |
| return "I couldn't understand the audio. Please try again.", None | |
| # Gradio Interface | |
| with gr.Blocks() as jarvis_app: | |
| gr.Markdown("# Jarvis Assistant") | |
| gr.Markdown("A voice-based assistant application built with Gradio SDK.") | |
| with gr.Row(): | |
| audio_input = gr.Audio(type="filepath", label="Speak Your Query") # Removed 'source' argument | |
| text_output = gr.Textbox(label="Response") | |
| audio_output = gr.Audio(label="Voice Response") | |
| submit_button = gr.Button("Submit") | |
| submit_button.click(jarvis_assistant, inputs=audio_input, outputs=[text_output, audio_output]) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| jarvis_app.launch() | |