Wewoo commited on
Commit
27fb8c8
·
verified ·
1 Parent(s): a77c81d

Upload 6 files

Browse files
Files changed (6) hide show
  1. .gitattributes +0 -1
  2. .gitignore +7 -0
  3. README.md +5 -5
  4. app.py +104 -92
  5. best.pt +3 -0
  6. requirements.txt +47 -6
.gitattributes CHANGED
@@ -25,7 +25,6 @@
25
  *.safetensors filter=lfs diff=lfs merge=lfs -text
26
  saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
  *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
  *.tflite filter=lfs diff=lfs merge=lfs -text
30
  *.tgz filter=lfs diff=lfs merge=lfs -text
31
  *.wasm filter=lfs diff=lfs merge=lfs -text
 
25
  *.safetensors filter=lfs diff=lfs merge=lfs -text
26
  saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
  *.tar.* filter=lfs diff=lfs merge=lfs -text
 
28
  *.tflite filter=lfs diff=lfs merge=lfs -text
29
  *.tgz filter=lfs diff=lfs merge=lfs -text
30
  *.wasm filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ flagged/
2
+ *.pt
3
+ *.png
4
+ *.jpg
5
+ *.mp4
6
+ *.mkv
7
+ gradio_cached_examples/
README.md CHANGED
@@ -1,10 +1,10 @@
1
  ---
2
- title: DRIVER DETECT
3
- emoji: 😻
4
- colorFrom: gray
5
- colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 6.0.2
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
1
  ---
2
+ title: Pothole Yolov8 Nano
3
+ emoji: 🌖
4
+ colorFrom: pink
5
+ colorTo: blue
6
  sdk: gradio
7
+ sdk_version: 3.16.1
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py CHANGED
@@ -1,92 +1,104 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
- import PyPDF2
4
-
5
- # Model tốt nhất cho tiếng Anh + chạy được trên HF Free
6
- summarizer = pipeline(
7
- "summarization",
8
- model="google/pegasus-xsum"
9
- )
10
-
11
- def read_pdf(file_obj):
12
- """Đọc PDF từ Gradio file object."""
13
- try:
14
- with open(file_obj.name, "rb") as f:
15
- reader = PyPDF2.PdfReader(f)
16
- text = ""
17
- for page in reader.pages:
18
- t = page.extract_text()
19
- if t:
20
- text += t + "\n"
21
- return text
22
- except Exception as e:
23
- return None
24
-
25
-
26
- def chunk_text(text, max_chars=512):
27
- """
28
- Chia text thành đoạn nhỏ an toàn cho Pegasus.
29
- Pegasus chịu tối đa ~512 tokens → nên dùng 512 chars để chắc chắn không crash.
30
- """
31
- chunks = []
32
- text = text.strip()
33
-
34
- while len(text) > max_chars:
35
- # tìm dấu chấm gần nhất để chia tự nhiên
36
- cut = text.rfind('.', 0, max_chars)
37
- if cut == -1:
38
- # không có dấu chấm → cắt cứng
39
- cut = max_chars
40
- chunk = text[:cut].strip()
41
- if len(chunk) > 0:
42
- chunks.append(chunk)
43
- text = text[cut:].strip()
44
-
45
- if len(text) > 0:
46
- chunks.append(text)
47
-
48
- return chunks
49
-
50
-
51
- def summarize_pdf(pdf_file):
52
- if pdf_file is None:
53
- return "Hãy upload một file PDF."
54
-
55
- text = read_pdf(pdf_file)
56
- if text is None:
57
- return "Lỗi đọc PDF — có thể file bị mã hóa hoặc không phải PDF chuẩn."
58
-
59
- text = text.strip()
60
- if len(text) < 50:
61
- return "PDF quá ngắn hoặc không có nội dung văn bản."
62
-
63
- # chia nhỏ text → tránh lỗi CPU + tránh timeout HF
64
- chunks = chunk_text(text)
65
- summaries = []
66
-
67
- for i, chunk in enumerate(chunks):
68
- try:
69
- result = summarizer(
70
- chunk,
71
- max_length=120, # phù hợp Pegasus
72
- min_length=20,
73
- do_sample=False
74
- )
75
- summaries.append(result[0]["summary_text"])
76
- except Exception as e:
77
- summaries.append(f"[Lỗi khi tóm tắt đoạn {i+1}]: {e}")
78
-
79
- # nối summaries lại
80
- return "\n\n".join(summaries)
81
-
82
-
83
- with gr.Blocks() as demo:
84
- gr.Markdown("## 📝 Tóm tắt PDF bằng AI (Pegasus + Gradio)")
85
-
86
- pdf = gr.File(label="Upload PDF", file_types=[".pdf"])
87
- output = gr.Textbox(lines=12, label="Kết quả tóm tắt")
88
-
89
- btn = gr.Button("Tóm tắt PDF")
90
- btn.click(fn=summarize_pdf, inputs=pdf, outputs=output)
91
-
92
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ import requests
4
+ import os
5
+
6
+ from ultralytics import YOLO
7
+
8
+ file_urls = [
9
+ 'https://www.dropbox.com/s/b5g97xo901zb3ds/pothole_example.jpg?dl=1',
10
+ 'https://www.dropbox.com/s/86uxlxxlm1iaexa/pothole_screenshot.png?dl=1',
11
+ 'https://www.dropbox.com/s/7sjfwncffg8xej2/video_7.mp4?dl=1'
12
+ ]
13
+
14
+ def download_file(url, save_name):
15
+ url = url
16
+ if not os.path.exists(save_name):
17
+ file = requests.get(url)
18
+ open(save_name, 'wb').write(file.content)
19
+
20
+ for i, url in enumerate(file_urls):
21
+ if 'mp4' in file_urls[i]:
22
+ download_file(
23
+ file_urls[i],
24
+ f"video.mp4"
25
+ )
26
+ else:
27
+ download_file(
28
+ file_urls[i],
29
+ f"image_{i}.jpg"
30
+ )
31
+
32
+ model = YOLO('best.pt')
33
+ path = [['image_0.jpg'], ['image_1.jpg']]
34
+ video_path = [['video.mp4']]
35
+
36
+ def show_preds_image(image_path):
37
+ image = cv2.imread(image_path)
38
+ outputs = model.predict(source=image_path)
39
+ results = outputs[0].cpu().numpy()
40
+ for i, det in enumerate(results.boxes.xyxy):
41
+ cv2.rectangle(
42
+ image,
43
+ (int(det[0]), int(det[1])),
44
+ (int(det[2]), int(det[3])),
45
+ color=(0, 0, 255),
46
+ thickness=2,
47
+ lineType=cv2.LINE_AA
48
+ )
49
+ return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
50
+
51
+ inputs_image = [
52
+ gr.components.Image(type="filepath", label="Input Image"),
53
+ ]
54
+ outputs_image = [
55
+ gr.components.Image(type="numpy", label="Output Image"),
56
+ ]
57
+ interface_image = gr.Interface(
58
+ fn=show_preds_image,
59
+ inputs=inputs_image,
60
+ outputs=outputs_image,
61
+ title="Pothole detector",
62
+ examples=path,
63
+ cache_examples=False,
64
+ )
65
+
66
+ def show_preds_video(video_path):
67
+ cap = cv2.VideoCapture(video_path)
68
+ while(cap.isOpened()):
69
+ ret, frame = cap.read()
70
+ if ret:
71
+ frame_copy = frame.copy()
72
+ outputs = model.predict(source=frame)
73
+ results = outputs[0].cpu().numpy()
74
+ for i, det in enumerate(results.boxes.xyxy):
75
+ cv2.rectangle(
76
+ frame_copy,
77
+ (int(det[0]), int(det[1])),
78
+ (int(det[2]), int(det[3])),
79
+ color=(0, 0, 255),
80
+ thickness=2,
81
+ lineType=cv2.LINE_AA
82
+ )
83
+ yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
84
+
85
+ inputs_video = [
86
+ gr.components.Video(type="filepath", label="Input Video"),
87
+
88
+ ]
89
+ outputs_video = [
90
+ gr.components.Image(type="numpy", label="Output Image"),
91
+ ]
92
+ interface_video = gr.Interface(
93
+ fn=show_preds_video,
94
+ inputs=inputs_video,
95
+ outputs=outputs_video,
96
+ title="Pothole detector",
97
+ examples=video_path,
98
+ cache_examples=False,
99
+ )
100
+
101
+ gr.TabbedInterface(
102
+ [interface_image, interface_video],
103
+ tab_names=['Image inference', 'Video inference']
104
+ ).queue().launch()
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ce9a23590d666fcca004d3a2330f3e30b229bbe9df0dbf4b0fd390c20fbe67fe
3
+ size 6233272
requirements.txt CHANGED
@@ -1,6 +1,47 @@
1
- transformers
2
- torch
3
- gradio
4
- PyPDF2
5
- sentencepiece
6
- accelerate
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Ultralytics requirements
2
+ # Usage: pip install -r requirements.txt
3
+
4
+ # Base ----------------------------------------
5
+ hydra-core>=1.2.0
6
+ matplotlib>=3.2.2
7
+ numpy>=1.18.5
8
+ opencv-python>=4.1.1
9
+ Pillow>=7.1.2
10
+ PyYAML>=5.3.1
11
+ requests>=2.23.0
12
+ scipy>=1.4.1
13
+ torch>=1.7.0
14
+ torchvision>=0.8.1
15
+ tqdm>=4.64.0
16
+ ultralytics
17
+
18
+ # Logging -------------------------------------
19
+ tensorboard>=2.4.1
20
+ # clearml
21
+ # comet
22
+
23
+ # Plotting ------------------------------------
24
+ pandas>=1.1.4
25
+ seaborn>=0.11.0
26
+
27
+ # Export --------------------------------------
28
+ # coremltools>=6.0 # CoreML export
29
+ # onnx>=1.12.0 # ONNX export
30
+ # onnx-simplifier>=0.4.1 # ONNX simplifier
31
+ # nvidia-pyindex # TensorRT export
32
+ # nvidia-tensorrt # TensorRT export
33
+ # scikit-learn==0.19.2 # CoreML quantization
34
+ # tensorflow>=2.4.1 # TF exports (-cpu, -aarch64, -macos)
35
+ # tensorflowjs>=3.9.0 # TF.js export
36
+ # openvino-dev # OpenVINO export
37
+
38
+ # Extras --------------------------------------
39
+ ipython # interactive notebook
40
+ psutil # system utilization
41
+ thop>=0.1.1 # FLOPs computation
42
+ # albumentations>=1.0.3
43
+ # pycocotools>=2.0.6 # COCO mAP
44
+ # roboflow
45
+
46
+ # HUB -----------------------------------------
47
+ GitPython>=3.1.24