Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,37 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import base64
|
|
|
|
|
|
|
| 3 |
from mistralai import Mistral, ImageURLChunk
|
| 4 |
|
| 5 |
client = Mistral(api_key="RJIqm5OvwoMvLeWrFdv5JBx26tLsSSK7")
|
| 6 |
|
| 7 |
def process_image(image_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
with open(image_path, "rb") as image:
|
| 9 |
encoded = base64.b64encode(image.read()).decode()
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
gr.Interface(
|
| 18 |
fn=process_image,
|
| 19 |
inputs=gr.Image(type="filepath"),
|
| 20 |
-
outputs="
|
| 21 |
).launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
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(image_path):
|
| 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
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return {"error": str(e)}
|
| 32 |
|
| 33 |
gr.Interface(
|
| 34 |
fn=process_image,
|
| 35 |
inputs=gr.Image(type="filepath"),
|
| 36 |
+
outputs="text" # لأننا هلأ عم نرجع json_string كسلسلة نصية
|
| 37 |
).launch(share=True)
|