Spaces:
Sleeping
Sleeping
sachin commited on
Commit ·
8b6e3dc
1
Parent(s): 02f5f75
init
Browse files- app.py +62 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import dwani
|
| 5 |
+
import os
|
| 6 |
+
import tempfile
|
| 7 |
+
|
| 8 |
+
dwani.api_key = os.getenv("DWANI_API_KEY")
|
| 9 |
+
dwani.api_base = os.getenv("DWANI_API_BASE_URL")
|
| 10 |
+
|
| 11 |
+
# Language options as simple array
|
| 12 |
+
language_options = ["english", "kannada", "hindi"]
|
| 13 |
+
|
| 14 |
+
def visual_query(image, src_lang, tgt_lang, prompt):
|
| 15 |
+
# Save PIL Image to a temporary file
|
| 16 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
| 17 |
+
image.save(temp_file.name, format="PNG") # Explicitly save as PNG
|
| 18 |
+
temp_file_path = temp_file.name
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
# Call the API with the file path
|
| 22 |
+
result = dwani.Vision.caption(
|
| 23 |
+
file_path=temp_file_path,
|
| 24 |
+
query=prompt,
|
| 25 |
+
src_lang=src_lang,
|
| 26 |
+
tgt_lang=tgt_lang
|
| 27 |
+
)
|
| 28 |
+
print(result)
|
| 29 |
+
return result
|
| 30 |
+
finally:
|
| 31 |
+
# Clean up the temporary file
|
| 32 |
+
os.unlink(temp_file_path)
|
| 33 |
+
|
| 34 |
+
# Create Gradio interface
|
| 35 |
+
iface = gr.Interface(
|
| 36 |
+
fn=visual_query,
|
| 37 |
+
inputs=[
|
| 38 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 39 |
+
gr.Dropdown(
|
| 40 |
+
choices=language_options,
|
| 41 |
+
label="Source Language",
|
| 42 |
+
value="english", # Default value
|
| 43 |
+
info="Select the source language for the query"
|
| 44 |
+
),
|
| 45 |
+
gr.Dropdown(
|
| 46 |
+
choices=language_options,
|
| 47 |
+
label="Target Language",
|
| 48 |
+
value="kannada", # Default value
|
| 49 |
+
info="Select the target language for the response"
|
| 50 |
+
),
|
| 51 |
+
gr.Textbox(
|
| 52 |
+
label="Prompt",
|
| 53 |
+
placeholder="e.g., describe the image"
|
| 54 |
+
)
|
| 55 |
+
],
|
| 56 |
+
outputs=gr.JSON(label="API Response"),
|
| 57 |
+
title="Visual Query API Interface",
|
| 58 |
+
description="Upload an image, select source and target languages, and provide a prompt to query the visual API."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
# Launch the interface
|
| 62 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
dwani
|