File size: 6,475 Bytes
85cec0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
import requests
import gradio as gr
from openai import OpenAI
from dotenv import load_dotenv
from helpers import format_images, format_videos, format_links, format_local_map, format_knowledge

# Load API key from .env
load_dotenv()
AIML_API_KEY = os.getenv('AIML_API_KEY')
API_URL = 'https://api.aimlapi.com'

# Bagoodex API client encapsulation
class BagoodexClient:
    def __init__(self, api_key=AIML_API_KEY, api_url=API_URL):
        self.api_key = api_key
        self.api_url = api_url
        self.client = OpenAI(base_url=self.api_url, api_key=self.api_key)

    def complete_chat(self, query):
        response = self.client.chat.completions.create(
            model="bagoodex/bagoodex-search-v1",
            messages=[{"role": "user", "content": query}],
        )
        followup_id = response.id  # unique ID for followup searches
        answer = response.choices[0].message.content
        return followup_id, answer

    def get_links(self, followup_id):
        headers = {"Authorization": f"Bearer {self.api_key}"}
        params = {"followup_id": followup_id}
        response = requests.get(f"{self.api_url}/v1/bagoodex/links", headers=headers, params=params)
        return response.json()

    def get_images(self, followup_id):
        headers = {"Authorization": f"Bearer {self.api_key}"}
        params = {"followup_id": followup_id}
        response = requests.get(f"{self.api_url}/v1/bagoodex/images", headers=headers, params=params)
        return response.json()

    def get_videos(self, followup_id):
        headers = {"Authorization": f"Bearer {self.api_key}"}
        params = {"followup_id": followup_id}
        response = requests.get(f"{self.api_url}/v1/bagoodex/videos", headers=headers, params=params)
        return response.json()

    def get_local_map(self, followup_id):
        headers = {"Authorization": f"Bearer {self.api_key}"}
        params = {"followup_id": followup_id}
        response = requests.get(f"{self.api_url}/v1/bagoodex/local-map", headers=headers, params=params)
        return response.json()

    def get_knowledge(self, followup_id):
        headers = {"Authorization": f"Bearer {self.api_key}"}
        params = {"followup_id": followup_id}
        response = requests.get(f"{self.api_url}/v1/bagoodex/knowledge", headers=headers, params=params)
        return response.json()

client = BagoodexClient()

# Callback functions
def chat_response(user_input, chat_history, followup_id):
    followup_id_new, answer = client.complete_chat(user_input)
    chat_history = chat_history or []
    chat_history.append((user_input, answer))
    return "", chat_history, followup_id_new

def perform_local_map_search(followup_id, chat_history):
    if not followup_id:
        return chat_history
    result = client.get_local_map(followup_id)
    formatted = format_local_map(result)
    chat_history.append((None, formatted))
    return chat_history

def perform_knowledge_search(followup_id, chat_history):
    if not followup_id:
        return chat_history
    result = client.get_knowledge(followup_id)
    formatted = format_knowledge(result)
    chat_history.append((None, formatted))
    return chat_history

def perform_image_search(followup_id):
    if not followup_id:
        return []
    result = client.get_images(followup_id)
    urls = format_images(result)
    return urls

def perform_video_search(followup_id):
    if not followup_id:
        return "No followup ID available."
    result = client.get_videos(followup_id)
    return format_videos(result)

def perform_links_search(followup_id):
    if not followup_id:
        return "No followup ID available."
    result = client.get_links(followup_id)
    return format_links(result)

# Custom CSS to help the chat component fill the available height
css = """
#chatbot {
    height: 100%;
}
"""

# Build UI
with gr.Blocks(css=css, fill_height=True) as demo:
    with gr.Row():
        # Left column: Chat interface
        with gr.Column(scale=3):
            chatbot = gr.Chatbot(label="Bagoodex Chat", elem_id="chatbot")
            # State variables: chat history and followup ID.
            chat_history_state = gr.State([])
            followup_state = gr.State(None)
            # Two small buttons placed above the input field:
            with gr.Row():
                btn_local_map = gr.Button("Local Map Search", variant="secondary", size="sm")
                btn_knowledge = gr.Button("Knowledge Base", variant="secondary", size="sm")
            # Chat input area: textbox and explicit submit button.
            with gr.Row():
                txt = gr.Textbox(placeholder="Enter your query here...", show_label=False, scale=7)
                submit_btn = gr.Button("Submit", scale=1)
            # Wire up the chat callbacks:
            submit_btn.click(chat_response, inputs=[txt, chat_history_state, followup_state],
                               outputs=[txt, chatbot, followup_state])
            txt.submit(chat_response, inputs=[txt, chat_history_state, followup_state],
                       outputs=[txt, chatbot, followup_state])
            # The two additional search buttons update the chat history by appending a new assistant message.
            btn_local_map.click(perform_local_map_search, inputs=[followup_state, chat_history_state],
                                outputs=[chatbot, chat_history_state])
            btn_knowledge.click(perform_knowledge_search, inputs=[followup_state, chat_history_state],
                                outputs=[chatbot, chat_history_state])
        # Right column: Advanced search options for images, videos, and links.
        with gr.Column(scale=1):
            gr.Markdown("### Advanced Search Options")
            with gr.Column(variant="panel"):
                btn_images = gr.Button("Search Images")
                btn_videos = gr.Button("Search Videos")
                btn_links = gr.Button("Search Links")
                gallery_output = gr.Gallery(label="Image Results", columns=2)
                video_output = gr.HTML(label="Video Results")
                links_output = gr.HTML(label="Links Results")
                btn_images.click(perform_image_search, inputs=[followup_state], outputs=[gallery_output])
                btn_videos.click(perform_video_search, inputs=[followup_state], outputs=[video_output])
                btn_links.click(perform_links_search, inputs=[followup_state], outputs=[links_output])
    demo.launch()