Spaces:
Runtime error
Runtime error
Commit
·
85cec0e
1
Parent(s):
a9f5708
first version working nice...
Browse files- .gitignore +6 -0
- app.py +150 -0
- bagoodex_client.py +67 -0
- helpers.py +43 -0
- requirements.txt +4 -0
.gitignore
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
prompts.md
|
| 2 |
+
.env
|
| 3 |
+
.venv
|
| 4 |
+
__pycache__
|
| 5 |
+
*.pyc
|
| 6 |
+
.DS_Store
|
app.py
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from helpers import format_images, format_videos, format_links, format_local_map, format_knowledge
|
| 7 |
+
|
| 8 |
+
# Load API key from .env
|
| 9 |
+
load_dotenv()
|
| 10 |
+
AIML_API_KEY = os.getenv('AIML_API_KEY')
|
| 11 |
+
API_URL = 'https://api.aimlapi.com'
|
| 12 |
+
|
| 13 |
+
# Bagoodex API client encapsulation
|
| 14 |
+
class BagoodexClient:
|
| 15 |
+
def __init__(self, api_key=AIML_API_KEY, api_url=API_URL):
|
| 16 |
+
self.api_key = api_key
|
| 17 |
+
self.api_url = api_url
|
| 18 |
+
self.client = OpenAI(base_url=self.api_url, api_key=self.api_key)
|
| 19 |
+
|
| 20 |
+
def complete_chat(self, query):
|
| 21 |
+
response = self.client.chat.completions.create(
|
| 22 |
+
model="bagoodex/bagoodex-search-v1",
|
| 23 |
+
messages=[{"role": "user", "content": query}],
|
| 24 |
+
)
|
| 25 |
+
followup_id = response.id # unique ID for followup searches
|
| 26 |
+
answer = response.choices[0].message.content
|
| 27 |
+
return followup_id, answer
|
| 28 |
+
|
| 29 |
+
def get_links(self, followup_id):
|
| 30 |
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
| 31 |
+
params = {"followup_id": followup_id}
|
| 32 |
+
response = requests.get(f"{self.api_url}/v1/bagoodex/links", headers=headers, params=params)
|
| 33 |
+
return response.json()
|
| 34 |
+
|
| 35 |
+
def get_images(self, followup_id):
|
| 36 |
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
| 37 |
+
params = {"followup_id": followup_id}
|
| 38 |
+
response = requests.get(f"{self.api_url}/v1/bagoodex/images", headers=headers, params=params)
|
| 39 |
+
return response.json()
|
| 40 |
+
|
| 41 |
+
def get_videos(self, followup_id):
|
| 42 |
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
| 43 |
+
params = {"followup_id": followup_id}
|
| 44 |
+
response = requests.get(f"{self.api_url}/v1/bagoodex/videos", headers=headers, params=params)
|
| 45 |
+
return response.json()
|
| 46 |
+
|
| 47 |
+
def get_local_map(self, followup_id):
|
| 48 |
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
| 49 |
+
params = {"followup_id": followup_id}
|
| 50 |
+
response = requests.get(f"{self.api_url}/v1/bagoodex/local-map", headers=headers, params=params)
|
| 51 |
+
return response.json()
|
| 52 |
+
|
| 53 |
+
def get_knowledge(self, followup_id):
|
| 54 |
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
| 55 |
+
params = {"followup_id": followup_id}
|
| 56 |
+
response = requests.get(f"{self.api_url}/v1/bagoodex/knowledge", headers=headers, params=params)
|
| 57 |
+
return response.json()
|
| 58 |
+
|
| 59 |
+
client = BagoodexClient()
|
| 60 |
+
|
| 61 |
+
# Callback functions
|
| 62 |
+
def chat_response(user_input, chat_history, followup_id):
|
| 63 |
+
followup_id_new, answer = client.complete_chat(user_input)
|
| 64 |
+
chat_history = chat_history or []
|
| 65 |
+
chat_history.append((user_input, answer))
|
| 66 |
+
return "", chat_history, followup_id_new
|
| 67 |
+
|
| 68 |
+
def perform_local_map_search(followup_id, chat_history):
|
| 69 |
+
if not followup_id:
|
| 70 |
+
return chat_history
|
| 71 |
+
result = client.get_local_map(followup_id)
|
| 72 |
+
formatted = format_local_map(result)
|
| 73 |
+
chat_history.append((None, formatted))
|
| 74 |
+
return chat_history
|
| 75 |
+
|
| 76 |
+
def perform_knowledge_search(followup_id, chat_history):
|
| 77 |
+
if not followup_id:
|
| 78 |
+
return chat_history
|
| 79 |
+
result = client.get_knowledge(followup_id)
|
| 80 |
+
formatted = format_knowledge(result)
|
| 81 |
+
chat_history.append((None, formatted))
|
| 82 |
+
return chat_history
|
| 83 |
+
|
| 84 |
+
def perform_image_search(followup_id):
|
| 85 |
+
if not followup_id:
|
| 86 |
+
return []
|
| 87 |
+
result = client.get_images(followup_id)
|
| 88 |
+
urls = format_images(result)
|
| 89 |
+
return urls
|
| 90 |
+
|
| 91 |
+
def perform_video_search(followup_id):
|
| 92 |
+
if not followup_id:
|
| 93 |
+
return "No followup ID available."
|
| 94 |
+
result = client.get_videos(followup_id)
|
| 95 |
+
return format_videos(result)
|
| 96 |
+
|
| 97 |
+
def perform_links_search(followup_id):
|
| 98 |
+
if not followup_id:
|
| 99 |
+
return "No followup ID available."
|
| 100 |
+
result = client.get_links(followup_id)
|
| 101 |
+
return format_links(result)
|
| 102 |
+
|
| 103 |
+
# Custom CSS to help the chat component fill the available height
|
| 104 |
+
css = """
|
| 105 |
+
#chatbot {
|
| 106 |
+
height: 100%;
|
| 107 |
+
}
|
| 108 |
+
"""
|
| 109 |
+
|
| 110 |
+
# Build UI
|
| 111 |
+
with gr.Blocks(css=css, fill_height=True) as demo:
|
| 112 |
+
with gr.Row():
|
| 113 |
+
# Left column: Chat interface
|
| 114 |
+
with gr.Column(scale=3):
|
| 115 |
+
chatbot = gr.Chatbot(label="Bagoodex Chat", elem_id="chatbot")
|
| 116 |
+
# State variables: chat history and followup ID.
|
| 117 |
+
chat_history_state = gr.State([])
|
| 118 |
+
followup_state = gr.State(None)
|
| 119 |
+
# Two small buttons placed above the input field:
|
| 120 |
+
with gr.Row():
|
| 121 |
+
btn_local_map = gr.Button("Local Map Search", variant="secondary", size="sm")
|
| 122 |
+
btn_knowledge = gr.Button("Knowledge Base", variant="secondary", size="sm")
|
| 123 |
+
# Chat input area: textbox and explicit submit button.
|
| 124 |
+
with gr.Row():
|
| 125 |
+
txt = gr.Textbox(placeholder="Enter your query here...", show_label=False, scale=7)
|
| 126 |
+
submit_btn = gr.Button("Submit", scale=1)
|
| 127 |
+
# Wire up the chat callbacks:
|
| 128 |
+
submit_btn.click(chat_response, inputs=[txt, chat_history_state, followup_state],
|
| 129 |
+
outputs=[txt, chatbot, followup_state])
|
| 130 |
+
txt.submit(chat_response, inputs=[txt, chat_history_state, followup_state],
|
| 131 |
+
outputs=[txt, chatbot, followup_state])
|
| 132 |
+
# The two additional search buttons update the chat history by appending a new assistant message.
|
| 133 |
+
btn_local_map.click(perform_local_map_search, inputs=[followup_state, chat_history_state],
|
| 134 |
+
outputs=[chatbot, chat_history_state])
|
| 135 |
+
btn_knowledge.click(perform_knowledge_search, inputs=[followup_state, chat_history_state],
|
| 136 |
+
outputs=[chatbot, chat_history_state])
|
| 137 |
+
# Right column: Advanced search options for images, videos, and links.
|
| 138 |
+
with gr.Column(scale=1):
|
| 139 |
+
gr.Markdown("### Advanced Search Options")
|
| 140 |
+
with gr.Column(variant="panel"):
|
| 141 |
+
btn_images = gr.Button("Search Images")
|
| 142 |
+
btn_videos = gr.Button("Search Videos")
|
| 143 |
+
btn_links = gr.Button("Search Links")
|
| 144 |
+
gallery_output = gr.Gallery(label="Image Results", columns=2)
|
| 145 |
+
video_output = gr.HTML(label="Video Results")
|
| 146 |
+
links_output = gr.HTML(label="Links Results")
|
| 147 |
+
btn_images.click(perform_image_search, inputs=[followup_state], outputs=[gallery_output])
|
| 148 |
+
btn_videos.click(perform_video_search, inputs=[followup_state], outputs=[video_output])
|
| 149 |
+
btn_links.click(perform_links_search, inputs=[followup_state], outputs=[links_output])
|
| 150 |
+
demo.launch()
|
bagoodex_client.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
load_dotenv()
|
| 7 |
+
API_KEY = os.getenv("AIML_API_KEY")
|
| 8 |
+
API_URL = "https://api.aimlapi.com"
|
| 9 |
+
|
| 10 |
+
class BagoodexClient:
|
| 11 |
+
def __init__(self, api_key=API_KEY, api_url=API_URL):
|
| 12 |
+
self.api_key = api_key
|
| 13 |
+
self.api_url = api_url
|
| 14 |
+
self.client = OpenAI(base_url=self.api_url, api_key=self.api_key)
|
| 15 |
+
|
| 16 |
+
def complete_chat(self, query):
|
| 17 |
+
"""
|
| 18 |
+
Calls the standard chat completion endpoint using the provided query.
|
| 19 |
+
Returns the generated followup ID and the text response.
|
| 20 |
+
"""
|
| 21 |
+
response = self.client.chat.completions.create(
|
| 22 |
+
model="bagoodex/bagoodex-search-v1",
|
| 23 |
+
messages=[{"role": "user", "content": query}],
|
| 24 |
+
)
|
| 25 |
+
followup_id = response.id # the unique ID for follow-up searches
|
| 26 |
+
answer = response.choices[0].message.content
|
| 27 |
+
return followup_id, answer
|
| 28 |
+
|
| 29 |
+
def get_links(self, followup_id):
|
| 30 |
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
| 31 |
+
params = {"followup_id": followup_id}
|
| 32 |
+
response = requests.get(
|
| 33 |
+
f"{self.api_url}/v1/bagoodex/links", headers=headers, params=params
|
| 34 |
+
)
|
| 35 |
+
return response.json()
|
| 36 |
+
|
| 37 |
+
def get_images(self, followup_id):
|
| 38 |
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
| 39 |
+
params = {"followup_id": followup_id}
|
| 40 |
+
response = requests.get(
|
| 41 |
+
f"{self.api_url}/v1/bagoodex/images", headers=headers, params=params
|
| 42 |
+
)
|
| 43 |
+
return response.json()
|
| 44 |
+
|
| 45 |
+
def get_videos(self, followup_id):
|
| 46 |
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
| 47 |
+
params = {"followup_id": followup_id}
|
| 48 |
+
response = requests.get(
|
| 49 |
+
f"{self.api_url}/v1/bagoodex/videos", headers=headers, params=params
|
| 50 |
+
)
|
| 51 |
+
return response.json()
|
| 52 |
+
|
| 53 |
+
def get_local_map(self, followup_id):
|
| 54 |
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
| 55 |
+
params = {"followup_id": followup_id}
|
| 56 |
+
response = requests.get(
|
| 57 |
+
f"{self.api_url}/v1/bagoodex/local-map", headers=headers, params=params
|
| 58 |
+
)
|
| 59 |
+
return response.json()
|
| 60 |
+
|
| 61 |
+
def get_knowledge(self, followup_id):
|
| 62 |
+
headers = {"Authorization": f"Bearer {self.api_key}"}
|
| 63 |
+
params = {"followup_id": followup_id}
|
| 64 |
+
response = requests.get(
|
| 65 |
+
f"{self.api_url}/v1/bagoodex/knowledge", headers=headers, params=params
|
| 66 |
+
)
|
| 67 |
+
return response.json()
|
helpers.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Helper formatting functions
|
| 2 |
+
def format_local_map(result):
|
| 3 |
+
html = (
|
| 4 |
+
f"<div><strong>Local Map:</strong><br>"
|
| 5 |
+
f"<a href='{result.get('link','')}' target='_blank'>View on Google Maps</a><br>"
|
| 6 |
+
f"<img src='{result.get('image','')}' style='width:100%;'/></div>"
|
| 7 |
+
)
|
| 8 |
+
return html
|
| 9 |
+
|
| 10 |
+
def format_knowledge(result):
|
| 11 |
+
html = (
|
| 12 |
+
f"<div><strong>{result.get('title','Unknown')}</strong><br>"
|
| 13 |
+
f"Type: {result.get('type','')}<br>"
|
| 14 |
+
f"Born: {result.get('born','')}<br>"
|
| 15 |
+
f"Died: {result.get('died','')}</div>"
|
| 16 |
+
)
|
| 17 |
+
return html
|
| 18 |
+
|
| 19 |
+
def format_images(result):
|
| 20 |
+
# Extract 'original' URL from each image dict.
|
| 21 |
+
urls = [item.get("original", "") for item in result]
|
| 22 |
+
return urls
|
| 23 |
+
|
| 24 |
+
def format_videos(result):
|
| 25 |
+
html = "<div>"
|
| 26 |
+
for vid in result:
|
| 27 |
+
html += (
|
| 28 |
+
f"<div style='margin-bottom:10px;'>"
|
| 29 |
+
f"<video controls style='width:100%;' src='{vid.get('link','')}'></video><br>"
|
| 30 |
+
f"{vid.get('title','')}"
|
| 31 |
+
f"</div>"
|
| 32 |
+
)
|
| 33 |
+
html += "</div>"
|
| 34 |
+
return html
|
| 35 |
+
|
| 36 |
+
def format_links(result):
|
| 37 |
+
html = "<div><ul>"
|
| 38 |
+
for url in result:
|
| 39 |
+
# Use the final part of the URL as the title.
|
| 40 |
+
title = url.rstrip('/').split('/')[-1]
|
| 41 |
+
html += f"<li><a href='{url}' target='_blank'>{title}</a></li>"
|
| 42 |
+
html += "</ul></div>"
|
| 43 |
+
return html
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
gradio
|
| 3 |
+
python-dotenv
|
| 4 |
+
requests
|