Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ import re
|
|
| 5 |
import gradio as gr
|
| 6 |
import google.generativeai as genai
|
| 7 |
|
| 8 |
-
#
|
| 9 |
print("Google Generative AI SDK version:", genai.__version__)
|
| 10 |
|
| 11 |
# ==== API KEY ROTATION ====
|
|
@@ -14,7 +14,7 @@ API_KEYS = [
|
|
| 14 |
"AIzaSyBfJeK_IAkfLpmnBsMNe6xwjcielrloSFY",
|
| 15 |
"AIzaSyCprm_rLChQ7Rv7YkHKI_6tcbS213PiPto",
|
| 16 |
"AIzaSyAPFCgH8uSjANmPRF9iHYIYcneTOod8Qi0",
|
| 17 |
-
"AIzaSyBbK-1P3JD6HPyE3QLhkOps6_-Xo3wUFbs"
|
| 18 |
]
|
| 19 |
|
| 20 |
key_index = 0
|
|
@@ -34,59 +34,64 @@ def extract_json(text):
|
|
| 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:
|
| 41 |
try:
|
| 42 |
return json.loads(json_text[first:last+1])
|
| 43 |
-
except:
|
| 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 |
-
#
|
| 54 |
api_key = get_next_key()
|
| 55 |
genai.configure(api_key=api_key)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
image.save(tmp.name)
|
| 67 |
elif isinstance(image, str):
|
| 68 |
-
with open(image,
|
| 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
|
| 77 |
-
uploaded_file = genai.upload_file(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
#
|
| 80 |
-
|
| 81 |
-
# display_name=os.path.basename(temp_file),
|
| 82 |
-
# mime_type="image/png",
|
| 83 |
-
|
| 84 |
-
#rag_store_name ="default_rag_store"
|
| 85 |
-
#)
|
| 86 |
-
print(f"Uploaded to Gemini: {uploaded_file.name}")
|
| 87 |
|
| 88 |
-
# ===
|
| 89 |
-
response = model.generate_content(
|
|
|
|
|
|
|
|
|
|
| 90 |
print(f"Got response: {response.text[:200]}...")
|
| 91 |
|
| 92 |
result = extract_json(response.text)
|
|
@@ -94,12 +99,10 @@ def process_image(image, prompt):
|
|
| 94 |
|
| 95 |
except Exception as e:
|
| 96 |
print(f"Error: {e}")
|
| 97 |
-
import traceback
|
| 98 |
-
traceback.print_exc()
|
| 99 |
return {"error": str(e)}
|
| 100 |
|
| 101 |
finally:
|
| 102 |
-
# Cleanup
|
| 103 |
if temp_file and os.path.exists(temp_file):
|
| 104 |
os.remove(temp_file)
|
| 105 |
print(f"Cleaned temp file: {temp_file}")
|
|
@@ -118,8 +121,8 @@ demo = gr.Interface(
|
|
| 118 |
gr.Textbox(lines=5, placeholder="Enter your prompt here...", label="Prompt"),
|
| 119 |
],
|
| 120 |
outputs=gr.JSON(label="Response"),
|
| 121 |
-
title="OCR & Analyzer",
|
| 122 |
-
description="Upload an image + prompt
|
| 123 |
flagging_mode="never",
|
| 124 |
)
|
| 125 |
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
import google.generativeai as genai
|
| 7 |
|
| 8 |
+
# ==== DEBUG VERSION ====
|
| 9 |
print("Google Generative AI SDK version:", genai.__version__)
|
| 10 |
|
| 11 |
# ==== API KEY ROTATION ====
|
|
|
|
| 14 |
"AIzaSyBfJeK_IAkfLpmnBsMNe6xwjcielrloSFY",
|
| 15 |
"AIzaSyCprm_rLChQ7Rv7YkHKI_6tcbS213PiPto",
|
| 16 |
"AIzaSyAPFCgH8uSjANmPRF9iHYIYcneTOod8Qi0",
|
| 17 |
+
"AIzaSyBbK-1P3JD6HPyE3QLhkOps6_-Xo3wUFbs",
|
| 18 |
]
|
| 19 |
|
| 20 |
key_index = 0
|
|
|
|
| 34 |
|
| 35 |
try:
|
| 36 |
return json.loads(json_text)
|
| 37 |
+
except Exception:
|
| 38 |
+
first, last = json_text.find("{"), json_text.rfind("}")
|
|
|
|
| 39 |
if first != -1 and last != -1 and last > first:
|
| 40 |
try:
|
| 41 |
return json.loads(json_text[first:last+1])
|
| 42 |
+
except Exception:
|
| 43 |
pass
|
| 44 |
return {"raw_response": text}
|
| 45 |
|
| 46 |
# ==== MAIN PROCESS ====
|
| 47 |
def process_image(image, prompt):
|
| 48 |
+
temp_file = None
|
| 49 |
+
uploaded_file = None
|
| 50 |
+
|
| 51 |
try:
|
| 52 |
print(f"Received image: {type(image)}")
|
| 53 |
print(f"Received prompt: {prompt}")
|
| 54 |
|
| 55 |
+
# === API key rotation ===
|
| 56 |
api_key = get_next_key()
|
| 57 |
genai.configure(api_key=api_key)
|
| 58 |
|
| 59 |
+
# === Ensure RAG store exists ===
|
| 60 |
+
RAG_STORE_NAME = "default_rag_store"
|
| 61 |
+
try:
|
| 62 |
+
genai.create_rag_store(name=RAG_STORE_NAME)
|
| 63 |
+
except Exception as e:
|
| 64 |
+
print(f"(Info) RAG store may already exist: {e}")
|
| 65 |
|
| 66 |
+
# === Create temp file ===
|
| 67 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp:
|
| 68 |
+
if hasattr(image, "save"):
|
| 69 |
image.save(tmp.name)
|
| 70 |
elif isinstance(image, str):
|
| 71 |
+
with open(image, "rb") as f:
|
| 72 |
tmp.write(f.read())
|
| 73 |
else:
|
| 74 |
raise ValueError(f"Unsupported image type: {type(image)}")
|
| 75 |
temp_file = tmp.name
|
|
|
|
| 76 |
print(f"Created temp file: {temp_file}")
|
| 77 |
|
| 78 |
+
# === Upload to Gemini with RAG ===
|
| 79 |
+
uploaded_file = genai.upload_file(
|
| 80 |
+
path=temp_file,
|
| 81 |
+
display_name=os.path.basename(temp_file),
|
| 82 |
+
mime_type="image/png",
|
| 83 |
+
rag_store_name=RAG_STORE_NAME, # ✅ thêm dòng này
|
| 84 |
+
)
|
| 85 |
+
print(f"Uploaded to Gemini RAG store: {uploaded_file.name}")
|
| 86 |
|
| 87 |
+
# === Create model ===
|
| 88 |
+
model = genai.GenerativeModel("gemini-2.5-flash")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# === Generate content using RAG context ===
|
| 91 |
+
response = model.generate_content(
|
| 92 |
+
[prompt],
|
| 93 |
+
request_options={"rag_store_name": RAG_STORE_NAME}, # ✅ cho phép truy vấn dựa theo store
|
| 94 |
+
)
|
| 95 |
print(f"Got response: {response.text[:200]}...")
|
| 96 |
|
| 97 |
result = extract_json(response.text)
|
|
|
|
| 99 |
|
| 100 |
except Exception as e:
|
| 101 |
print(f"Error: {e}")
|
| 102 |
+
import traceback; traceback.print_exc()
|
|
|
|
| 103 |
return {"error": str(e)}
|
| 104 |
|
| 105 |
finally:
|
|
|
|
| 106 |
if temp_file and os.path.exists(temp_file):
|
| 107 |
os.remove(temp_file)
|
| 108 |
print(f"Cleaned temp file: {temp_file}")
|
|
|
|
| 121 |
gr.Textbox(lines=5, placeholder="Enter your prompt here...", label="Prompt"),
|
| 122 |
],
|
| 123 |
outputs=gr.JSON(label="Response"),
|
| 124 |
+
title="Gemini OCR & Analyzer (RAG Enhanced)",
|
| 125 |
+
description="Upload an image + prompt → analyze using Gemini 2.5 with RAG store",
|
| 126 |
flagging_mode="never",
|
| 127 |
)
|
| 128 |
|