Tiktok-Talent-Info/ui.py

100 lines
4.1 KiB
Python
Raw Normal View History

2025-01-23 21:50:55 +08:00
import asyncio
2025-01-24 14:11:46 +08:00
import gradio as gr
from gradio_image_prompter import ImagePrompter
2025-01-23 21:50:55 +08:00
from endpoints.text import text_query
from endpoints.image import image_query
from endpoints.video import video_query
2025-01-24 14:11:46 +08:00
import torch
print("Available GPUs:", torch.cuda.device_count())
print("Visible Devices:", [torch.cuda.get_device_name(i) for i in range(torch.cuda.device_count())])
2025-01-23 21:50:55 +08:00
def setup_ui():
with gr.Blocks() as ui:
gr.Markdown(
"""
# Multimodal Query Interface
Submit text, image, or video queries and get insights powered by APIs.
"""
)
# Tabbed layout
with gr.Tabs():
# Text Query Tab
with gr.Tab("Text Query"):
gr.Markdown("### Submit a Text Query")
with gr.Row():
text_input = gr.Textbox(label="Your Question", placeholder="Type your question here...")
text_button = gr.Button("Submit")
text_output = gr.Textbox(label="Response", interactive=False)
text_button.click(
fn=lambda q: asyncio.run(text_query(q)),
inputs=[text_input],
outputs=[text_output]
)
# Image Query Tab
with gr.Tab("Image Query"):
gr.Markdown("### Submit an Image Query")
with gr.Row():
2025-01-24 14:11:46 +08:00
image_prompter = ImagePrompter(show_label=False)
2025-01-23 21:50:55 +08:00
image_question_input = gr.Textbox(label="Your Question", placeholder="Type your question here...")
image_button = gr.Button("Submit")
image_output = gr.Textbox(label="Response", interactive=False)
2025-01-24 14:11:46 +08:00
# async def handle_image_query(prompts, question):
# response = await image_query(prompts["image"], question)
# return response["response"] if "response" in response else response["error"]
async def handle_image_query(prompts, question):
"""
Handles the image query and ensures that inputs are valid.
"""
try:
# Validate prompts
if prompts is None or "image" not in prompts:
return "No image provided. Please upload an image."
image_data = prompts["image"]
# Check if image_data is valid
if image_data is None:
return "Invalid image input. Please upload a valid image."
# Call the `image_query` function
response = await image_query(image_data, question)
return response["response"] if "response" in response else response["error"]
except Exception as e:
return str(e)
2025-01-23 21:50:55 +08:00
image_button.click(
2025-01-24 14:11:46 +08:00
fn=handle_image_query,
inputs=[image_prompter, image_question_input],
2025-01-23 21:50:55 +08:00
outputs=[image_output]
)
# Video Query Tab
with gr.Tab("Video Query"):
gr.Markdown("### Submit a Video Query")
with gr.Row():
2025-01-24 14:11:46 +08:00
video_input = gr.Video(label="Upload Video")
2025-01-23 21:50:55 +08:00
video_question_input = gr.Textbox(label="Your Question", placeholder="Type your question here...")
video_button = gr.Button("Submit")
video_output = gr.Textbox(label="Response", interactive=False)
2025-01-24 14:11:46 +08:00
async def handle_video_query(video, question):
response = await video_query(video, question)
return response.get("responses", response.get("error", "Error processing video."))
2025-01-23 21:50:55 +08:00
video_button.click(
2025-01-24 14:11:46 +08:00
fn=handle_video_query,
2025-01-23 21:50:55 +08:00
inputs=[video_input, video_question_input],
outputs=[video_output]
)
return ui
if __name__ == "__main__":
ui = setup_ui()
2025-01-24 14:11:46 +08:00
ui.launch(server_name="0.0.0.0", server_port=8002)