Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from PIL import Image | |
| import os | |
| from chatbot_updated import ( | |
| chatbot_updated, | |
| detect_artifact, | |
| text_to_speech, | |
| cleanup_audio_file | |
| ) | |
| import os | |
| print("CURRENT DIR:", os.getcwd()) | |
| print("FILES:", os.listdir()) | |
| print("DATA EXISTS:", os.path.exists("data")) | |
| # ========================= | |
| # 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() |