Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,58 @@
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
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 |
-
|
45 |
-
|
46 |
-
|
47 |
-
""
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
پردازش وظیفه با بارگذاری موقت مدل.
|
56 |
-
"""
|
57 |
-
self.load_model(task, model_id)
|
58 |
-
model = self.models[task]
|
59 |
-
if task == "translation":
|
60 |
-
text = kwargs.get("text", "")
|
61 |
-
return model(text)
|
62 |
-
elif task == "qa":
|
63 |
-
question = kwargs.get("question", "")
|
64 |
-
context = kwargs.get("context", "")
|
65 |
-
return model(question=question, context=context)
|
66 |
-
else:
|
67 |
-
raise ValueError(f"Unsupported task: {task}")
|
68 |
|
69 |
if __name__ == "__main__":
|
70 |
-
|
71 |
-
|
72 |
-
"qa": "distilbert-base-uncased-distilled-squad", # مدل فشرده
|
73 |
-
}
|
74 |
-
|
75 |
-
tasks = [
|
76 |
-
{"task": "translation", "model_id": MODEL_CONFIG["translation"], "kwargs": {"text": "سلام دنیا!"}},
|
77 |
-
{"task": "qa", "model_id": MODEL_CONFIG["qa"], "kwargs": {"question": "What is AI?", "context": "AI is artificial intelligence."}}
|
78 |
-
]
|
79 |
-
|
80 |
-
system = MultiModelSystem(memory_limit_gb=15)
|
81 |
-
|
82 |
-
for task_info in tasks:
|
83 |
-
try:
|
84 |
-
system.check_memory_usage()
|
85 |
-
result = system.process_task(task_info["task"], task_info["model_id"], **task_info["kwargs"])
|
86 |
-
print(f"Result for task '{task_info['task']}':", result)
|
87 |
-
except Exception as e:
|
88 |
-
print(f"Error during task '{task_info['task']}':", str(e))
|
89 |
-
finally:
|
90 |
-
system.unload_model(task_info["task"])
|
|
|
1 |
import os
|
2 |
+
from datasets import load_dataset
|
3 |
+
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
4 |
+
import customtkinter as ctk
|
5 |
+
from tkinter import filedialog
|
6 |
+
from PIL import Image, ImageTk
|
7 |
+
|
8 |
+
|
9 |
+
dataset = load_dataset("hezarai/parsynth-ocr-200k")
|
10 |
+
# تنظیم GUI
|
11 |
+
ctk.set_appearance_mode("System")
|
12 |
+
ctk.set_default_color_theme("blue")
|
13 |
+
|
14 |
+
class OCRApp(ctk.CTk):
|
15 |
+
def __init__(self):
|
16 |
+
super().__init__()
|
17 |
+
self.title("OCR with Hugging Face")
|
18 |
+
self.geometry("800x600")
|
19 |
+
|
20 |
+
# مدل و پردازشگر
|
21 |
+
self.processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-handwritten")
|
22 |
+
self.model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-handwritten")
|
23 |
+
|
24 |
+
# عناصر رابط کاربری
|
25 |
+
self.image_label = ctk.CTkLabel(self, text="No Image Selected", width=400, height=300, corner_radius=8)
|
26 |
+
self.image_label.pack(pady=20)
|
27 |
+
|
28 |
+
self.upload_button = ctk.CTkButton(self, text="Upload Image", command=self.upload_image)
|
29 |
+
self.upload_button.pack(pady=10)
|
30 |
+
|
31 |
+
self.result_label = ctk.CTkTextbox(self, height=200)
|
32 |
+
self.result_label.pack(pady=10, fill="both", expand=True)
|
33 |
+
|
34 |
+
def upload_image(self):
|
35 |
+
file_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png;*.jpg;*.jpeg")])
|
36 |
+
if not file_path:
|
37 |
+
return
|
38 |
+
|
39 |
+
# نمایش تصویر
|
40 |
+
image = Image.open(file_path).resize((400, 300))
|
41 |
+
self.image_tk = ImageTk.PhotoImage(image)
|
42 |
+
self.image_label.configure(image=self.image_tk, text="")
|
43 |
+
|
44 |
+
# انجام OCR
|
45 |
+
text = self.perform_ocr(file_path)
|
46 |
+
self.result_label.delete("1.0", "end")
|
47 |
+
self.result_label.insert("1.0", text)
|
48 |
+
|
49 |
+
def perform_ocr(self, image_path):
|
50 |
+
image = Image.open(image_path).convert("RGB")
|
51 |
+
pixel_values = self.processor(images=image, return_tensors="pt").pixel_values
|
52 |
+
generated_ids = self.model.generate(pixel_values)
|
53 |
+
generated_text = self.processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
54 |
+
return generated_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
if __name__ == "__main__":
|
57 |
+
app = OCRApp()
|
58 |
+
app.mainloop()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|