| import gradio as gr |
| from PIL import Image |
| import os |
|
|
| from chatbot_updated import ( |
| chatbot_updated, |
| detect_artifact, |
| text_to_speech, |
| cleanup_audio_file |
| ) |
|
|
|
|
| |
| |
| |
| def process(input_text, image, audio): |
| """ |
| Handles text + image + audio input |
| """ |
|
|
| |
| |
| |
| question = input_text |
|
|
| if audio is not None: |
| |
| audio_bytes = open(audio, "rb").read() |
| question = audio_bytes |
|
|
| |
| |
| |
| 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 |
|
|
| |
| |
| |
| answer = chatbot_updated(question, image=img) |
| answer = str(answer).strip() |
|
|
| |
| |
| |
| audio_file = text_to_speech(answer) |
| audio_out = None |
|
|
| if audio_file and os.path.exists(audio_file): |
| audio_out = audio_file |
|
|
| |
| |
| |
| return ( |
| answer, |
| annotated, |
| audio_out |
| ) |
|
|
|
|
| |
| |
| |
| 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") |
|
|
| |
| |
| |
| btn.click( |
| fn=process, |
| inputs=[input_text, image_input, audio_input], |
| outputs=[output_text, output_image, output_audio] |
| ) |
|
|
| |
| |
| |
| if __name__ == "__main__": |
| app.launch( |
| server_name="0.0.0.0", |
| server_port=7860 |
| ) |