chatbot / app.py
fatma812's picture
Update app.py
8a109b5 verified
Raw
History Blame Contribute Delete
2.86 kB
import gradio as gr
from PIL import Image
import os
from chatbot_updated import (
chatbot_updated,
detect_artifact,
text_to_speech,
cleanup_audio_file
)
# =========================
# CORE FUNCTION
# =========================
def process(input_text, image, audio):
"""
Handles text + image + audio input
"""
# =========================
# 1. PRIORITY: AUDIO
# =========================
question = input_text
if audio is not None:
# gradio gives file path for audio
audio_bytes = open(audio, "rb").read()
question = audio_bytes
# =========================
# 2. IMAGE HANDLING
# =========================
img = None
detected_name = None
if image is not None:
img = Image.open(image).convert("RGB")
detected_name, annotated = detect_artifact(img)
else:
annotated = None
# =========================
# 3. CHATBOT CALL
# =========================
answer = chatbot_updated(question, image=img)
answer = str(answer).strip()
# =========================
# 4. TEXT TO SPEECH
# =========================
audio_file = text_to_speech(answer)
audio_out = None
if audio_file and os.path.exists(audio_file):
audio_out = audio_file
# =========================
# RETURN UI OUTPUTS
# =========================
return (
answer, # text output
annotated, # image output
audio_out # audio output
)
# =========================
# GRADIO UI
# =========================
with gr.Blocks(title="🏺 Egyptian Artifact Chatbot") as app:
gr.Markdown("# 🏺 Egyptian Artifact Chatbot (Gradio Version)")
gr.Markdown("Ask using text, voice, or image")
with gr.Row():
with gr.Column():
input_text = gr.Textbox(label="πŸ’¬ Ask a question")
audio_input = gr.Audio(
sources=["microphone"],
type="filepath",
label="🎀 Voice Input",
)
image_input = gr.Image(
type="filepath",
label="πŸ“· Upload Image"
)
btn = gr.Button("πŸš€ Get Answer")
with gr.Column():
output_text = gr.Textbox(label="πŸ€– Answer")
output_image = gr.Image(label="πŸ–Ό Detected Artifact")
output_audio = gr.Audio(label="πŸ”Š Speech Output")
# =========================
# BUTTON CLICK
# =========================
btn.click(
fn=process,
inputs=[input_text, image_input, audio_input],
outputs=[output_text, output_image, output_audio]
)
# =========================
# RUN APP
# =========================
if __name__ == "__main__":
app.launch(
server_name="0.0.0.0",
server_port=7860
)