abdullah-1111 commited on
Commit
f103579
·
verified ·
1 Parent(s): 0f64c89

Delete gemini_requestcr4.py

Browse files
Files changed (1) hide show
  1. gemini_requestcr4.py +0 -135
gemini_requestcr4.py DELETED
@@ -1,135 +0,0 @@
1
- import base64
2
- import json
3
- import re
4
- import requests
5
- import os
6
-
7
- # Your Gemini API key
8
- API_KEY = "your_gemini_api_key_here"
9
-
10
-
11
- #Path to the document image you want to extract data from
12
- image_path = r"C:\Users\ASUS\Downloads\CR_OCR_Cls.v4-original_images.folder\train\CR4\CS000130_CR_jpg.rf.b2528e60546ec399c3088ac69c6a66e2.jpg"
13
-
14
- with open(image_path, "rb") as f:
15
- image_b64 = base64.b64encode(f.read()).decode()
16
-
17
- prompt = """
18
- Extract the following fields from the CR4 document image. Return both Arabic and English text where available:
19
-
20
- الرقم الموحد
21
- رقم المنشأة
22
- التاريخ
23
-
24
- الاسم التجاري للشركة
25
- نوعها
26
- جنسيتها
27
- مدة الشركة
28
- تبدأ من
29
- وتنتهي في
30
- مركزها الرئيسي
31
- هاتف
32
- الرمز البريدي
33
-
34
- النشاط
35
- رأس المال
36
- المديرون
37
- سلطات المدير/المديرون
38
- يشهد مكتب السجل التجاري بمدينة
39
- بأنه تم تسجيل المؤسسة المذكورة أعلاة بمدينة
40
- وتنتهي صلاحية الشهادات في
41
- بموجب الإيصال رقم
42
- وتاريخ
43
-
44
- Return as JSON with keys:
45
- {
46
- "رقم_موحد": ...,
47
- "رقم_المنشأة": ...,
48
- "التاريخ": ...,
49
- "الاسم_التجاري": ...,
50
- "نوعها": ...,
51
- "جنسيتها": ...,
52
- "مدة_الشركة": ...,
53
- "تبدأ_من": ...,
54
- "تنتهي_في": ...,
55
- "مركزها_الرئيسي": ...,
56
- "هاتف": ...,
57
- "الرمز_البريدي": ...,
58
- "النشاط": ...,
59
- "رأس_المال": ...,
60
- "المديرون": ...,
61
- "سلطات_المدير": ...,
62
- "يشهد_مكتب_السجل": ...,
63
- "تم_تسجيل_المؤسسة": ...,
64
- "تنتهي_صلاحية_الشهادة": ...,
65
- "الإيصال_رقم": ...,
66
- "الإيصال_تاريخ": ...
67
- }
68
- If a field is missing, set it to null.
69
- """
70
-
71
- url = f"https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash:generateContent?key={API_KEY}"
72
-
73
- headers = {
74
- "Content-Type": "application/json"
75
- }
76
-
77
- data = {
78
- "contents": [
79
- {
80
- "role": "user",
81
- "parts": [
82
- {"text": prompt},
83
- {
84
- "inline_data": {
85
- "mime_type": "image/jpeg",
86
- "data": image_b64
87
- }
88
- }
89
- ]
90
- }
91
- ]
92
- }
93
-
94
- response = requests.post(url, headers=headers, json=data)
95
-
96
- try:
97
- response_text = response.json()['candidates'][0]['content']['parts'][0]['text']
98
-
99
- # استخراج JSON من النص
100
- match = re.search(r"```json\s*(\{.*?\})\s*```", response_text, re.DOTALL)
101
- if match:
102
- json_text = match.group(1)
103
- result = json.loads(json_text)
104
-
105
- base_name = os.path.splitext(os.path.basename(image_path))[0]
106
- folder = os.path.dirname(image_path)
107
- json_path = os.path.join(folder, f"{base_name}.json")
108
-
109
- # إذا الملف موجود نقرأه ونضيف عليه
110
- if os.path.exists(json_path):
111
- with open(json_path, "r", encoding="utf-8") as f:
112
- try:
113
- existing_data = json.load(f)
114
- if not isinstance(existing_data, list):
115
- existing_data = [existing_data]
116
- except json.JSONDecodeError:
117
- existing_data = []
118
- else:
119
- existing_data = []
120
-
121
- existing_data.append(result)
122
-
123
- with open(json_path, "w", encoding="utf-8") as f:
124
- json.dump(existing_data, f, ensure_ascii=False, indent=2)
125
-
126
- print(f"✅ تم حفظ النتيجة في الملف: {json_path}")
127
- print(json.dumps(result, indent=2, ensure_ascii=False))
128
- else:
129
- print("❌ لم أتمكن من استخراج JSON نظيف من الرد:")
130
- print(response_text)
131
-
132
- except Exception as e:
133
- print(f"❌ حدث خطأ أثناء المعالجة: {e}")
134
- print("الرد الكامل من Gemini:")
135
- print(response.text)