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

Delete gemini_requestCR7.py

Browse files
Files changed (1) hide show
  1. gemini_requestCR7.py +0 -103
gemini_requestCR7.py DELETED
@@ -1,103 +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
- #Path to the document image you want to extract data from
11
- image_path = r"C:\Users\ASUS\Downloads\OneDrive_1_7-30-2025\CR7\CS015431_CR.jpg"
12
-
13
- # قراءة الصورة وتحويلها إلى base64
14
- with open(image_path, "rb") as f:
15
- image_b64 = base64.b64encode(f.read()).decode()
16
-
17
- prompt = """
18
- يرجى استخراج الحقول التالية من مستند السجل التجاري (CR7) بالصورة، باللغة العربية فقط:
19
-
20
- - اسم المنشأة
21
- - نوع السجل
22
- - حالة السجل
23
- - الرقم الموحد للمنشأة
24
- - رقم السجل التجاري
25
- - اسم المالك
26
- - نوع الكيان
27
- - تاريخ الاصدار
28
- - تاريخ الانتهاء
29
- - قائمة المدراء
30
- - المدينة
31
- - الموقع الإلكتروني
32
- - الانشطة التجارية
33
-
34
- أرجو إعادة النتيجة بصيغة JSON بهذه المفاتيح فقط، وإذا أي حقل غير موجود فضع قيمته null:
35
-
36
- {
37
- "اسم المنشأة": null,
38
- "نوع السجل": null,
39
- "حالة السجل": null,
40
- "الرقم الموحد للمنشأة": null,
41
- "رقم السجل التجاري": null,
42
- "اسم المالك": null,
43
- "نوع الكيان": null,
44
- "تاريخ الاصدار": null,
45
- "تاريخ الانتهاء": null,
46
- "قائمة المدراء": null,
47
- "المدينة": null,
48
- "الموقع الإلكتروني": null,
49
- "الانشطة التجارية": null
50
- }
51
- """
52
-
53
- url = f"https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash:generateContent?key={API_KEY}"
54
-
55
- headers = {
56
- "Content-Type": "application/json"
57
- }
58
-
59
- data = {
60
- "contents": [
61
- {
62
- "role": "user",
63
- "parts": [
64
- {"text": prompt},
65
- {
66
- "inline_data": {
67
- "mime_type": "image/jpeg",
68
- "data": image_b64
69
- }
70
- }
71
- ]
72
- }
73
- ]
74
- }
75
-
76
- response = requests.post(url, headers=headers, json=data)
77
-
78
- try:
79
- response_text = response.json()['candidates'][0]['content']['parts'][0]['text']
80
-
81
- match = re.search(r"```json\s*(\{.*?\})\s*```", response_text, re.DOTALL)
82
- if match:
83
- json_text = match.group(1)
84
- result = json.loads(json_text)
85
-
86
- # حفظ النتيجة بنفس اسم ملف الصورة وداخل نفس المجلد
87
- folder = os.path.dirname(image_path)
88
- base_name = os.path.splitext(os.path.basename(image_path))[0]
89
- output_file = os.path.join(folder, base_name + ".json")
90
-
91
- with open(output_file, "w", encoding="utf-8") as f:
92
- json.dump(result, f, ensure_ascii=False, indent=2)
93
-
94
- print(f"✅ تم حفظ النتيجة في ملف: {output_file}")
95
- print(json.dumps(result, indent=2, ensure_ascii=False))
96
- else:
97
- print("❌ لم أتمكن من استخراج JSON نظيف من الرد:")
98
- print(response_text)
99
-
100
- except Exception as e:
101
- print(f"❌ حدث خطأ أثناء المعالجة: {e}")
102
- print("🔴 الرد الكامل من Gemini:")
103
- print(response.text)