Update app.py
Browse files
app.py
CHANGED
|
@@ -5,17 +5,19 @@ import io
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
from zhipuai import ZhipuAI
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
API_KEY = "d659608f7d5d42b1821a9303fc50b618.NesCuIytwpxjxpBJ"
|
| 11 |
-
|
| 12 |
-
# Khởi tạo ZhipuAI client
|
| 13 |
client = ZhipuAI(api_key=API_KEY)
|
| 14 |
|
| 15 |
def extract_text(image):
|
| 16 |
if isinstance(image, np.ndarray): # Chuyển NumPy array thành ảnh
|
| 17 |
image = Image.fromarray(image)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
# Chuyển đổi ảnh sang Base64
|
| 20 |
buffered = io.BytesIO()
|
| 21 |
image.save(buffered, format="PNG")
|
|
@@ -47,7 +49,8 @@ def extract_text(image):
|
|
| 47 |
|
| 48 |
return text, output_text_file, output_image_file
|
| 49 |
else:
|
| 50 |
-
|
|
|
|
| 51 |
|
| 52 |
demo = gr.Interface(
|
| 53 |
fn=extract_text,
|
|
@@ -62,4 +65,4 @@ demo = gr.Interface(
|
|
| 62 |
)
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
-
demo.launch()
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import numpy as np
|
| 7 |
from zhipuai import ZhipuAI
|
| 8 |
+
import os
|
| 9 |
|
| 10 |
+
# Lấy API Key từ biến môi trường
|
| 11 |
+
API_KEY = os.getenv("ZHIPUAI_API_KEY", "d659608f7d5d42b1821a9303fc50b618.NesCuIytwpxjxpBJ")
|
|
|
|
|
|
|
| 12 |
client = ZhipuAI(api_key=API_KEY)
|
| 13 |
|
| 14 |
def extract_text(image):
|
| 15 |
if isinstance(image, np.ndarray): # Chuyển NumPy array thành ảnh
|
| 16 |
image = Image.fromarray(image)
|
| 17 |
|
| 18 |
+
# Giới hạn kích thước ảnh
|
| 19 |
+
image = image.resize((min(image.width, 1024), min(image.height, 1024)), Image.Resampling.LANCZOS)
|
| 20 |
+
|
| 21 |
# Chuyển đổi ảnh sang Base64
|
| 22 |
buffered = io.BytesIO()
|
| 23 |
image.save(buffered, format="PNG")
|
|
|
|
| 49 |
|
| 50 |
return text, output_text_file, output_image_file
|
| 51 |
else:
|
| 52 |
+
error_msg = f"Lỗi: {str(response)}" if response else "Lỗi: Không kết nối được API."
|
| 53 |
+
return error_msg, None, None
|
| 54 |
|
| 55 |
demo = gr.Interface(
|
| 56 |
fn=extract_text,
|
|
|
|
| 65 |
)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
+
demo.launch()
|