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