Spaces:
Sleeping
Sleeping
import gradio as gr | |
from transformers import pipeline | |
from gtts import gTTS | |
import os | |
from pydub import AudioSegment | |
from pydub.playback import play | |
# Load a public model for question answering | |
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased-distilled-squad") | |
# Function to generate the answer and dinosaur speech | |
def answer_question(question): | |
context = "Provide the context or text where the answer should be found." | |
result = qa_pipeline(question=question, context=context) | |
answer = result['answer'] | |
# Generate speech | |
tts = gTTS(text=answer, lang='en') | |
tts.save("answer.mp3") | |
# Play the speech | |
audio = AudioSegment.from_mp3("answer.mp3") | |
play(audio) | |
return answer, "answer.mp3" | |
# HTML for the dinosaur image as background | |
image_html = """ | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<style> | |
body { | |
margin: 0; | |
height: 100vh; | |
background-image: url('https://i.pinimg.com/736x/b6/e8/61/b6e86149d3180b11018d9c3de1af92e6.jpg'); | |
background-size: cover; | |
background-position: center; | |
background-repeat: no-repeat; | |
} | |
</style> | |
</head> | |
<body> | |
</body> | |
</html> | |
""" | |
# Define Gradio interface | |
iface = gr.Blocks() | |
with iface: | |
gr.Markdown( | |
""" | |
# Ancient Interface with Modern Dinosaur | |
Ask a question and get an answer from our modern dinosaur! | |
""" | |
) | |
with gr.Row(): | |
question_input = gr.Textbox(label="Ask a Question") | |
answer_text = gr.Textbox(label="Answer") | |
audio_output = gr.Audio(label="Dinosaur Speech") | |
question_input.change(answer_question, inputs=question_input, outputs=[answer_text, audio_output]) | |
gr.HTML(image_html) | |
# Launch the interface | |
iface.launch(share=True) | |