Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
import json
|
| 4 |
+
import re
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import google.generativeai as genai
|
| 7 |
+
|
| 8 |
+
# API Keys
|
| 9 |
+
API_KEYS = [
|
| 10 |
+
"AIzaSyAtHYSw1iWJYyGLt_hC5mKO3627wkHtN-s",
|
| 11 |
+
"AIzaSyBfJeK_IAkfLpmnBsMNe6xwjcielrloSFY",
|
| 12 |
+
"AIzaSyCprm_rLChQ7Rv7YkHKI_6tcbS213PiPto",
|
| 13 |
+
"AIzaSyAPFCgH8uSjANmPRF9iHYIYcneTOod8Qi0"
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
key_index = 0
|
| 17 |
+
|
| 18 |
+
def get_next_key():
|
| 19 |
+
global key_index
|
| 20 |
+
key = API_KEYS[key_index % len(API_KEYS)]
|
| 21 |
+
key_index += 1
|
| 22 |
+
return key
|
| 23 |
+
|
| 24 |
+
def extract_json(text):
|
| 25 |
+
# Tìm JSON trong ```json blocks
|
| 26 |
+
match = re.search(r"```json\s*(.*?)\s*```", text, re.IGNORECASE | re.DOTALL)
|
| 27 |
+
if match:
|
| 28 |
+
json_text = match.group(1).strip()
|
| 29 |
+
else:
|
| 30 |
+
json_text = text.strip()
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
return json.loads(json_text)
|
| 34 |
+
except:
|
| 35 |
+
# Tìm {...} ngoài cùng
|
| 36 |
+
first = json_text.find("{")
|
| 37 |
+
last = json_text.rfind("}")
|
| 38 |
+
if first != -1 and last != -1 and last > first:
|
| 39 |
+
try:
|
| 40 |
+
return json.loads(json_text[first:last+1])
|
| 41 |
+
except:
|
| 42 |
+
pass
|
| 43 |
+
return {"raw_response": text}
|
| 44 |
+
|
| 45 |
+
def process_image(image, prompt):
|
| 46 |
+
try:
|
| 47 |
+
print(f"Received image: {type(image)}")
|
| 48 |
+
print(f"Received prompt: {prompt}")
|
| 49 |
+
|
| 50 |
+
# Lấy API key
|
| 51 |
+
api_key = get_next_key()
|
| 52 |
+
genai.configure(api_key=api_key)
|
| 53 |
+
|
| 54 |
+
# Tạo model
|
| 55 |
+
model = genai.GenerativeModel("gemini-2.5-flash")
|
| 56 |
+
|
| 57 |
+
# Upload ảnh
|
| 58 |
+
temp_file = None
|
| 59 |
+
uploaded_file = None
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
# Tạo file tạm
|
| 63 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp:
|
| 64 |
+
if hasattr(image, 'save'):
|
| 65 |
+
# PIL Image object
|
| 66 |
+
image.save(tmp.name)
|
| 67 |
+
elif isinstance(image, str):
|
| 68 |
+
# File path
|
| 69 |
+
with open(image, 'rb') as f:
|
| 70 |
+
tmp.write(f.read())
|
| 71 |
+
else:
|
| 72 |
+
raise ValueError(f"Unsupported image type: {type(image)}")
|
| 73 |
+
temp_file = tmp.name
|
| 74 |
+
|
| 75 |
+
print(f"Created temp file: {temp_file}")
|
| 76 |
+
|
| 77 |
+
# Upload lên Gemini
|
| 78 |
+
uploaded_file = genai.upload_file(temp_file)
|
| 79 |
+
print(f"Uploaded to Gemini: {uploaded_file.name}")
|
| 80 |
+
|
| 81 |
+
# Gọi API
|
| 82 |
+
response = model.generate_content([prompt, uploaded_file])
|
| 83 |
+
print(f"Got response: {response.text[:100]}...")
|
| 84 |
+
|
| 85 |
+
# Extract JSON
|
| 86 |
+
result = extract_json(response.text)
|
| 87 |
+
return result
|
| 88 |
+
|
| 89 |
+
finally:
|
| 90 |
+
# Cleanup
|
| 91 |
+
if temp_file and os.path.exists(temp_file):
|
| 92 |
+
os.remove(temp_file)
|
| 93 |
+
print(f"Cleaned temp file: {temp_file}")
|
| 94 |
+
if uploaded_file:
|
| 95 |
+
genai.delete_file(uploaded_file.name)
|
| 96 |
+
print(f"Deleted from Gemini: {uploaded_file.name}")
|
| 97 |
+
|
| 98 |
+
except Exception as e:
|
| 99 |
+
print(f"Error: {e}")
|
| 100 |
+
import traceback
|
| 101 |
+
traceback.print_exc()
|
| 102 |
+
return {"error": str(e)}
|
| 103 |
+
|
| 104 |
+
# Gradio Interface
|
| 105 |
+
demo = gr.Interface(
|
| 106 |
+
fn=process_image,
|
| 107 |
+
inputs=[
|
| 108 |
+
gr.File(label="Upload Image", file_types=["image"]),
|
| 109 |
+
gr.Textbox(lines=5, placeholder="Enter your prompt here...", label="Prompt"),
|
| 110 |
+
],
|
| 111 |
+
outputs=gr.JSON(label="Response"),
|
| 112 |
+
title="Gemini OCR API",
|
| 113 |
+
description="Upload image + prompt to get JSON response",
|
| 114 |
+
flagging_mode="never",
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
demo.api_name = "/predict"
|
| 118 |
+
|
| 119 |
+
if __name__ == "__main__":
|
| 120 |
+
demo.launch(
|
| 121 |
+
server_name="0.0.0.0",
|
| 122 |
+
server_port=int(os.getenv("PORT", "7860")),
|
| 123 |
+
show_error=True,
|
| 124 |
+
debug=True,
|
| 125 |
+
# share=True,
|
| 126 |
+
# inbrowser=True,
|
| 127 |
+
)
|