apaxray commited on
Commit
f3b54e3
·
verified ·
1 Parent(s): da280fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -87
app.py CHANGED
@@ -1,90 +1,58 @@
1
  import os
2
- import gc
3
- import psutil
4
- from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
5
-
6
- class MultiModelSystem:
7
- """
8
- سیستم چندمدلی با مدیریت حافظه و ذخیره‌سازی موقت مدل‌ها در دیسک.
9
- """
10
-
11
- def __init__(self, memory_limit_gb=15):
12
- self.models = {}
13
- self.memory_limit_gb = memory_limit_gb
14
- self.model_cache_dir = "model_cache"
15
- os.makedirs(self.model_cache_dir, exist_ok=True)
16
-
17
- def check_memory_usage(self):
18
- mem = psutil.virtual_memory()
19
- used_gb = mem.used / (1024 ** 3)
20
- print(f"Memory usage: {mem.percent}% ({used_gb:.2f} GB used)")
21
- if used_gb > self.memory_limit_gb:
22
- raise MemoryError(f"Memory limit exceeded: {used_gb:.2f} GB used (limit: {self.memory_limit_gb} GB)")
23
-
24
- def load_model(self, task, model_id):
25
- """
26
- بارگذاری مدل از کش یا ذخیره‌سازی.
27
- """
28
- cache_path = os.path.join(self.model_cache_dir, f"{task}.bin")
29
- if task not in self.models:
30
- self.check_memory_usage()
31
- print(f"Loading model for task '{task}'...")
32
- if os.path.exists(cache_path):
33
- print(f"Loading model from cache: {cache_path}")
34
- self.models[task] = joblib.load(cache_path)
35
- else:
36
- model = AutoModelForSeq2SeqLM.from_pretrained(
37
- model_id, torch_dtype="auto", low_cpu_mem_usage=True
38
- )
39
- tokenizer = AutoTokenizer.from_pretrained(model_id)
40
- self.models[task] = pipeline("translation", model=model, tokenizer=tokenizer)
41
- joblib.dump(self.models[task], cache_path)
42
- print(f"Model cached at {cache_path}")
43
-
44
- def unload_model(self, task):
45
- """
46
- تخلیه مدل از حافظه.
47
- """
48
- if task in self.models:
49
- print(f"Unloading model for task '{task}'...")
50
- del self.models[task]
51
- gc.collect()
52
-
53
- def process_task(self, task, model_id, **kwargs):
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
- MODEL_CONFIG = {
71
- "translation": "Helsinki-NLP/opus-mt-en-ro", # مدل سبک‌تر
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()