File size: 2,859 Bytes
1aeba5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d30190
3c0e4d5
1aeba5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
544b0ab
8a109b5
 
 
 
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
)