mimoha commited on
Commit
8bbb2b3
·
verified ·
1 Parent(s): 8054a47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -7
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
- data_url = f"data:image/jpeg;base64,{encoded}"
11
- response = client.ocr.process(
12
- document=ImageURLChunk(image_url=data_url),
13
- model="mistral-ocr-latest"
14
- )
15
- return response.to_dict()
 
 
 
 
 
 
 
 
 
 
16
 
17
  gr.Interface(
18
  fn=process_image,
19
  inputs=gr.Image(type="filepath"),
20
- outputs="json"
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)