Spaces:
Runtime error
Runtime error
initial commit
Browse files- app.py +148 -0
- app.py:Zone.Identifier +0 -0
- requirements.txt +69 -0
- requirements.txt:Zone.Identifier +0 -0
app.py
ADDED
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import google.generativeai as genai
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
+
import json
|
6 |
+
|
7 |
+
# Your API key - for Hugging Face, set this as a secret in the Space settings
|
8 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") # This will get the API key from environment variables
|
9 |
+
|
10 |
+
# Configure Gemini API
|
11 |
+
def configure_genai():
|
12 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
13 |
+
model = genai.GenerativeModel('gemini-1.5-flash-8b')
|
14 |
+
return model
|
15 |
+
|
16 |
+
def process_form(image):
|
17 |
+
try:
|
18 |
+
model = configure_genai()
|
19 |
+
|
20 |
+
if not isinstance(image, Image.Image):
|
21 |
+
image = Image.open(image)
|
22 |
+
|
23 |
+
prompt = """
|
24 |
+
لطفاً این تصویر فرم را تحلیل کنید و اطلاعات زیر را در قالب JSON استخراج کنید:
|
25 |
+
1. تمام فیلدهای متنی و مقادیر آنها
|
26 |
+
2. چک باکسها یا دکمههای رادیویی و وضعیت آنها
|
27 |
+
3. امضاها یا مهرها در صورت وجود
|
28 |
+
|
29 |
+
لطفاً پاسخ را به صورت یک شیء JSON با نام فیلدها و مقادیر مناسب فرمت کنید.
|
30 |
+
"""
|
31 |
+
|
32 |
+
response = model.generate_content([prompt, image])
|
33 |
+
response_text = response.text
|
34 |
+
json_start = response_text.find('{')
|
35 |
+
json_end = response_text.rfind('}') + 1
|
36 |
+
|
37 |
+
if json_start != -1 and json_end != -1:
|
38 |
+
json_str = response_text[json_start:json_end]
|
39 |
+
parsed_json = json.loads(json_str)
|
40 |
+
return json.dumps(parsed_json, indent=2, ensure_ascii=False)
|
41 |
+
else:
|
42 |
+
return response.text
|
43 |
+
|
44 |
+
except Exception as e:
|
45 |
+
return f"خطا در پردازش فرم: {str(e)}"
|
46 |
+
|
47 |
+
def create_interface():
|
48 |
+
css = """
|
49 |
+
body {
|
50 |
+
background-color: #000000;
|
51 |
+
}
|
52 |
+
|
53 |
+
.gradio-container {
|
54 |
+
color: white !important;
|
55 |
+
direction: rtl !important;
|
56 |
+
text-align: right !important;
|
57 |
+
}
|
58 |
+
|
59 |
+
.title-text {
|
60 |
+
color: white !important;
|
61 |
+
text-align: right !important;
|
62 |
+
}
|
63 |
+
|
64 |
+
.subtitle-text {
|
65 |
+
color: white !important;
|
66 |
+
text-align: right !important;
|
67 |
+
}
|
68 |
+
|
69 |
+
.description-text {
|
70 |
+
color: white !important;
|
71 |
+
text-align: right !important;
|
72 |
+
}
|
73 |
+
|
74 |
+
.label-wrap span {
|
75 |
+
color: white !important;
|
76 |
+
text-align: right !important;
|
77 |
+
}
|
78 |
+
|
79 |
+
.md h1, .md h2, .md h3, .md p, .md ol, .md li {
|
80 |
+
color: white !important;
|
81 |
+
text-align: right !important;
|
82 |
+
direction: rtl !important;
|
83 |
+
}
|
84 |
+
|
85 |
+
button {
|
86 |
+
direction: rtl !important;
|
87 |
+
background-color: #ff4b1f !important;
|
88 |
+
color: white !important;
|
89 |
+
}
|
90 |
+
|
91 |
+
.output-markdown {
|
92 |
+
direction: rtl !important;
|
93 |
+
text-align: right !important;
|
94 |
+
}
|
95 |
+
|
96 |
+
textarea {
|
97 |
+
direction: rtl !important;
|
98 |
+
text-align: right !important;
|
99 |
+
background-color: #2d2d2d !important;
|
100 |
+
color: white !important;
|
101 |
+
}
|
102 |
+
"""
|
103 |
+
|
104 |
+
with gr.Blocks(title="سیستم استخراج متن از فرم - نسخه نمایشی هایمارت", css=css, theme="darker") as interface:
|
105 |
+
gr.Markdown('<h1 class="title-text">سیستم هوشمند استخراج متن از فرم</h1>')
|
106 |
+
gr.Markdown('<h3 class="subtitle-text">نسخه نمایشی برای هایمارت</h3>')
|
107 |
+
gr.Markdown('<p class="description-text">لطفاً تصویر فرم خود را بارگذاری کنید تا اطلاعات آن به صورت خودکار استخراج شود</p>')
|
108 |
+
|
109 |
+
with gr.Row():
|
110 |
+
with gr.Column():
|
111 |
+
image_input = gr.Image(
|
112 |
+
label="تصویر فرم را اینجا بارگذاری کنید",
|
113 |
+
type="pil"
|
114 |
+
)
|
115 |
+
submit_btn = gr.Button("پردازش فرم")
|
116 |
+
|
117 |
+
with gr.Column():
|
118 |
+
output_text = gr.Textbox(
|
119 |
+
label="اطلاعات استخراج شده",
|
120 |
+
lines=10,
|
121 |
+
rtl=True
|
122 |
+
)
|
123 |
+
|
124 |
+
submit_btn.click(
|
125 |
+
fn=process_form,
|
126 |
+
inputs=[image_input],
|
127 |
+
outputs=output_text
|
128 |
+
)
|
129 |
+
|
130 |
+
gr.Markdown("""
|
131 |
+
<div class="guide-section">
|
132 |
+
<h3>راهنما:</h3>
|
133 |
+
<ol>
|
134 |
+
<li>یک تصویر از فرم خود را بارگذاری کنید</li>
|
135 |
+
<li>روی دکمه 'پردازش فرم' کلیک کنید</li>
|
136 |
+
<li>نتیجه به صورت خودکار نمایش داده خواهد شد</li>
|
137 |
+
</ol>
|
138 |
+
|
139 |
+
<p><strong>نکته:</strong> برای نتیجه بهتر، لطفاً از تصاویر واضح و با نور کافی استفاده کنید.</p>
|
140 |
+
|
141 |
+
<p><em>این نسخه نمایشی صرفاً جهت ارزیابی قابلیتهای سیستم میباشد.</em></p>
|
142 |
+
</div>
|
143 |
+
""")
|
144 |
+
|
145 |
+
return interface
|
146 |
+
|
147 |
+
interface = create_interface()
|
148 |
+
interface.launch()
|
app.py:Zone.Identifier
ADDED
File without changes
|
requirements.txt
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
aiofiles==23.2.1
|
2 |
+
annotated-types==0.7.0
|
3 |
+
anyio==4.6.2.post1
|
4 |
+
cachetools==5.5.0
|
5 |
+
certifi==2024.8.30
|
6 |
+
charset-normalizer==3.4.0
|
7 |
+
click==8.1.7
|
8 |
+
fastapi==0.115.4
|
9 |
+
ffmpy==0.4.0
|
10 |
+
filelock==3.16.1
|
11 |
+
fsspec==2024.10.0
|
12 |
+
google-ai-generativelanguage==0.6.10
|
13 |
+
google-api-core==2.22.0
|
14 |
+
google-api-python-client==2.151.0
|
15 |
+
google-auth==2.35.0
|
16 |
+
google-auth-httplib2==0.2.0
|
17 |
+
google-generativeai==0.8.3
|
18 |
+
googleapis-common-protos==1.65.0
|
19 |
+
gradio==5.5.0
|
20 |
+
gradio_client==1.4.2
|
21 |
+
grpcio==1.67.1
|
22 |
+
grpcio-status==1.67.1
|
23 |
+
h11==0.14.0
|
24 |
+
httpcore==1.0.6
|
25 |
+
httplib2==0.22.0
|
26 |
+
httpx==0.27.2
|
27 |
+
huggingface-hub==0.26.2
|
28 |
+
idna==3.10
|
29 |
+
Jinja2==3.1.4
|
30 |
+
markdown-it-py==3.0.0
|
31 |
+
MarkupSafe==2.1.5
|
32 |
+
mdurl==0.1.2
|
33 |
+
numpy==2.1.3
|
34 |
+
orjson==3.10.11
|
35 |
+
packaging==24.1
|
36 |
+
pandas==2.2.3
|
37 |
+
pillow==11.0.0
|
38 |
+
proto-plus==1.25.0
|
39 |
+
protobuf==5.28.3
|
40 |
+
pyasn1==0.6.1
|
41 |
+
pyasn1_modules==0.4.1
|
42 |
+
pydantic==2.9.2
|
43 |
+
pydantic_core==2.23.4
|
44 |
+
pydub==0.25.1
|
45 |
+
Pygments==2.18.0
|
46 |
+
pyparsing==3.2.0
|
47 |
+
python-dateutil==2.9.0.post0
|
48 |
+
python-multipart==0.0.12
|
49 |
+
pytz==2024.2
|
50 |
+
PyYAML==6.0.2
|
51 |
+
requests==2.32.3
|
52 |
+
rich==13.9.4
|
53 |
+
rsa==4.9
|
54 |
+
ruff==0.7.2
|
55 |
+
safehttpx==0.1.1
|
56 |
+
semantic-version==2.10.0
|
57 |
+
shellingham==1.5.4
|
58 |
+
six==1.16.0
|
59 |
+
sniffio==1.3.1
|
60 |
+
starlette==0.41.2
|
61 |
+
tomlkit==0.12.0
|
62 |
+
tqdm==4.66.6
|
63 |
+
typer==0.12.5
|
64 |
+
typing_extensions==4.12.2
|
65 |
+
tzdata==2024.2
|
66 |
+
uritemplate==4.1.1
|
67 |
+
urllib3==2.2.3
|
68 |
+
uvicorn==0.32.0
|
69 |
+
websockets==12.0
|
requirements.txt:Zone.Identifier
ADDED
File without changes
|