Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,26 +3,25 @@ import base64
|
|
| 3 |
import mimetypes
|
| 4 |
import json
|
| 5 |
from mistralai import Mistral, ImageURLChunk
|
|
|
|
|
|
|
| 6 |
|
| 7 |
client = Mistral(api_key="RJIqm5OvwoMvLeWrFdv5JBx26tLsSSK7")
|
| 8 |
|
| 9 |
-
def process_image(
|
| 10 |
-
mime_type, _ = mimetypes.guess_type(image_path)
|
| 11 |
-
if mime_type is None:
|
| 12 |
-
return {"error": "Unsupported image type or cannot detect file type."}
|
| 13 |
-
|
| 14 |
-
with open(image_path, "rb") as image:
|
| 15 |
-
encoded = base64.b64encode(image.read()).decode()
|
| 16 |
-
|
| 17 |
-
data_url = f"data:{mime_type};base64,{encoded}"
|
| 18 |
-
|
| 19 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
response = client.ocr.process(
|
| 21 |
document=ImageURLChunk(image_url=data_url),
|
| 22 |
model="mistral-ocr-latest"
|
| 23 |
)
|
| 24 |
|
| 25 |
-
# تحويل النتيجة إلى JSON منسق
|
| 26 |
response_dict = json.loads(response.model_dump_json())
|
| 27 |
json_string = json.dumps(response_dict, indent=4, ensure_ascii=False)
|
| 28 |
return json_string
|
|
@@ -32,6 +31,6 @@ def process_image(image_path):
|
|
| 32 |
|
| 33 |
gr.Interface(
|
| 34 |
fn=process_image,
|
| 35 |
-
inputs=gr.Image(type="
|
| 36 |
-
outputs="text"
|
| 37 |
).launch(share=True, show_error=True)
|
|
|
|
| 3 |
import mimetypes
|
| 4 |
import json
|
| 5 |
from mistralai import Mistral, ImageURLChunk
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import io
|
| 8 |
|
| 9 |
client = Mistral(api_key="RJIqm5OvwoMvLeWrFdv5JBx26tLsSSK7")
|
| 10 |
|
| 11 |
+
def process_image(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
try:
|
| 13 |
+
# نحول الصورة من PIL إلى base64
|
| 14 |
+
buffered = io.BytesIO()
|
| 15 |
+
image.save(buffered, format="JPEG")
|
| 16 |
+
encoded = base64.b64encode(buffered.getvalue()).decode()
|
| 17 |
+
mime_type = "image/jpeg"
|
| 18 |
+
data_url = f"data:{mime_type};base64,{encoded}"
|
| 19 |
+
|
| 20 |
response = client.ocr.process(
|
| 21 |
document=ImageURLChunk(image_url=data_url),
|
| 22 |
model="mistral-ocr-latest"
|
| 23 |
)
|
| 24 |
|
|
|
|
| 25 |
response_dict = json.loads(response.model_dump_json())
|
| 26 |
json_string = json.dumps(response_dict, indent=4, ensure_ascii=False)
|
| 27 |
return json_string
|
|
|
|
| 31 |
|
| 32 |
gr.Interface(
|
| 33 |
fn=process_image,
|
| 34 |
+
inputs=gr.Image(type="pil"), # بدل filepath بـ pil
|
| 35 |
+
outputs="text"
|
| 36 |
).launch(share=True, show_error=True)
|