Spaces:
Running
Running
Download utils.py from suraj-ml-projects/Text-GPT: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/spaces/suraj-ml-projects/Text-GPT/resolve/main/utils.py
- Command line
-
hf download hf://spaces/suraj-ml-projects/Text-GPT/utils.py
-
curl -L -o utils.py https://huggingface.co/spaces/suraj-ml-projects/Text-GPT/resolve/main/utils.py
1.49 kB
| import cv2 | |
| import numpy as np | |
| from PIL import Image | |
| import tempfile | |
| import os | |
| def process_image(image) -> str: | |
| """Basic image description""" | |
| try: | |
| captioner = ModelLoader().load_image_captioner() # singleton-like | |
| result = captioner(image) | |
| return result[0]['generated_text'] | |
| except: | |
| return "Image processed. Describe what you see or ask a question about it." | |
| def process_video(video_path) -> str: | |
| """Extract key frames and describe""" | |
| cap = cv2.VideoCapture(video_path) | |
| frames_desc = [] | |
| frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) | |
| step = max(1, frame_count // 5) # ~5 key frames | |
| for i in range(0, frame_count, step): | |
| cap.set(cv2.CAP_PROP_POS_FRAMES, i) | |
| ret, frame = cap.read() | |
| if ret: | |
| pil_img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) | |
| desc = process_image(pil_img) | |
| frames_desc.append(f"Frame {i//step + 1}: {desc}") | |
| cap.release() | |
| return "\n".join(frames_desc) + "\n\nVideo summarized from key frames." | |
| def generate_references(query: str) -> str: | |
| """Fake realistic references (static or rule-based)""" | |
| refs = [ | |
| f"https://en.wikipedia.org/wiki/{query.replace(' ', '_')}", | |
| "https://arxiv.org/search/?query=" + query.replace(" ", "+"), | |
| "https://scholar.google.com/scholar?q=" + query.replace(" ", "+") | |
| ] | |
| return "\n".join([f"- [{r.split('//')[1].split('/')[0]}]({r})" for r in refs]) |