Spaces:
Runtime error
Runtime error
File size: 4,956 Bytes
85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d 85cec0e 954241d |
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 152 153 154 |
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
from bagoodex_client import BagoodexClient
# Load API key from .env
load_dotenv()
AIML_API_KEY = os.getenv('AIML_API_KEY')
API_URL = 'https://api.aimlapi.com'
client = BagoodexClient()
def format_knowledge(result):
title = result.get('title', 'Unknown')
type_ = result.get('type', '')
born = result.get('born', '')
died = result.get('died', '')
content = f"""
**{title}**
Type: {type_}
Born: {born}
Died: {died}
"""
return gr.Markdown(content)
def format_images(result):
urls = [item.get("original", "") for item in result]
return urls
# Helper formatting functions
def format_videos(result):
return [vid.get('link', '') for vid in result]
# Advanced search functions
def perform_video_search(followup_id):
if not followup_id:
return []
result = client.get_videos(followup_id)
return format_videos(result)
def format_links(result):
links_md = "**Links:**\n"
for url in result:
title = url.rstrip('/').split('/')[-1]
links_md += f"- [{title}]({url})\n"
return gr.Markdown(links_md)
# Define the chat function
def chat_function(message, history, followup_id):
followup_id_new, answer = client.complete_chat(message)
return answer, followup_id_new
def format_local_map(result):
link = result.get('link', '')
image_url = result.get('image', '')
html = f"""
<div>
<strong>Local Map:</strong><br>
<a href='{link}' target='_blank'>View on Google Maps</a><br>
<img src='{image_url}' style='width:100%;'/>
</div>
"""
return gr.HTML(html)
def append_local_map(followup_id, chatbot_value):
if not followup_id:
return chatbot_value
result = client.get_local_map(followup_id)
formatted = format_local_map(result)
new_message = {"role": "assistant", "content": formatted}
return chatbot_value + [new_message]
def append_knowledge(followup_id, chatbot_value):
if not followup_id:
return chatbot_value
result = client.get_knowledge(followup_id)
formatted = format_knowledge(result)
new_message = {"role": "assistant", "content": formatted}
return chatbot_value + [new_message]
# Define advanced search functions
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_links_search(followup_id):
if not followup_id:
return gr.Markdown("No followup ID available.")
result = client.get_links(followup_id)
return format_links(result)
# Custom CSS
css = """
#chatbot {
height: 100%;
}
"""
# Build UI
with gr.Blocks(css=css, fill_height=True) as demo:
followup_state = gr.State(None)
with gr.Row():
with gr.Column(scale=3):
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 = gr.ChatInterface(
fn=chat_function,
type="messages",
additional_inputs=[followup_state],
additional_outputs=[followup_state],
)
# Wire up the buttons to append to chat history
btn_local_map.click(
append_local_map,
inputs=[followup_state, chat.chatbot],
outputs=chat.chatbot
)
btn_knowledge.click(
append_knowledge,
inputs=[followup_state, chat.chatbot],
outputs=chat.chatbot
)
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.Gallery(label="Video Results", columns=1, visible=True)
links_output = gr.Markdown(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() |