File size: 2,157 Bytes
e424986
493d5e8
 
15ef175
 
 
 
493d5e8
62a8e44
 
 
 
 
 
493d5e8
 
 
 
 
 
 
 
 
246a8f4
493d5e8
246a8f4
493d5e8
246a8f4
 
 
493d5e8
 
 
 
 
15ef175
493d5e8
 
246a8f4
15ef175
246a8f4
493d5e8
15ef175
246a8f4
 
15ef175
246a8f4
493d5e8
246a8f4
493d5e8
246a8f4
 
 
 
 
 
 
 
 
 
28ecd3e
 
 
246a8f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import gradio as gr
import asyncio
import speech_recognition as sr
import logging

# Configure logging
logging.basicConfig(level=logging.INFO)

# Mock AI core for demonstration purposes
class AICore:
    async def generate_response(self, query, user_id):
        await asyncio.sleep(1)  # Simulate async processing
        return {"response": f"AI Response to: {query}"}

# Initialize the AI core
ai = AICore()

# Function to process the query and get the AI response
async def process_query(query):
    result = await ai.generate_response(query, 1)
    return result['response']

# Function to handle the text input submission
def submit_query(query, chat_history):
    if not query:
        return chat_history
    response = asyncio.run(process_query(query))
    chat_history.append({"role": "user", "content": query})
    chat_history.append({"role": "assistant", "content": response})
    return "", chat_history

# Function to handle voice input
def listen_voice_command():
    recognizer = sr.Recognizer()
    with sr.Microphone() as source:
        logging.info("Listening...")
        audio = recognizer.listen(source)
        try:
            query = recognizer.recognize_google(audio)
            logging.info(f"Voice input recognized: {query}")
            return query
        except sr.UnknownValueError:
            logging.error("Could not understand audio")
            return "Could not understand audio"
        except sr.RequestError as e:
            logging.error(f"Could not request results; {e}")
            return f"Could not request results; {e}"

# Gradio app
with gr.Blocks() as demo:
    chatbot = gr.Chatbot(type="messages")
    msg = gr.Textbox(label="Enter your message")
    voice_btn = gr.Button("Speak")
    clear = gr.Button("Clear")

    # Event listeners
    msg.submit(submit_query, [msg, chatbot], [msg, chatbot], queue=False)
    voice_btn.click(listen_voice_command, None, msg, queue=False)
    clear.click(lambda: None, None, chatbot, queue=False)

# Add the runtime error message to the app
gr.Error("runtime error\nNo @spaces.GPU function detected during startup\nContainer logs:")

demo.launch(show_error=True)