Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,78 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import json
|
| 4 |
-
import re
|
| 5 |
import uuid
|
| 6 |
from PIL import Image
|
| 7 |
from bs4 import BeautifulSoup
|
| 8 |
import requests
|
| 9 |
import random
|
| 10 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
def extract_text_from_webpage(html_content):
|
| 18 |
soup = BeautifulSoup(html_content, 'html.parser')
|
|
@@ -62,10 +122,9 @@ def respond(message, history):
|
|
| 62 |
|
| 63 |
# Handle image processing
|
| 64 |
if message["files"]:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
vqa += generate_caption_instructblip(image, message["text"])
|
| 69 |
|
| 70 |
# Define function metadata for user interface
|
| 71 |
functions_metadata = [
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import json
|
|
|
|
| 4 |
import uuid
|
| 5 |
from PIL import Image
|
| 6 |
from bs4 import BeautifulSoup
|
| 7 |
import requests
|
| 8 |
import random
|
| 9 |
+
from transformers import LlavaProcessor, LlavaForConditionalGeneration, TextIteratorStreamer
|
| 10 |
+
from threading import Thread
|
| 11 |
+
import re
|
| 12 |
+
import time
|
| 13 |
+
import torch
|
| 14 |
+
import cv2
|
| 15 |
+
|
| 16 |
+
model_id = "llava-hf/llava-interleave-qwen-0.5b-hf"
|
| 17 |
+
|
| 18 |
+
processor = LlavaProcessor.from_pretrained(model_id)
|
| 19 |
+
|
| 20 |
+
model = LlavaForConditionalGeneration.from_pretrained(model_id, low_cpu_mem_usage=True)
|
| 21 |
+
model.to("cpu")
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def sample_frames(video_file) :
|
| 25 |
+
try:
|
| 26 |
+
video = cv2.VideoCapture(video_file)
|
| 27 |
+
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 28 |
+
num_frames = 12
|
| 29 |
+
interval = total_frames // num_frames
|
| 30 |
+
frames = []
|
| 31 |
+
for i in range(total_frames):
|
| 32 |
+
ret, frame = video.read()
|
| 33 |
+
pil_img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 34 |
+
if not ret:
|
| 35 |
+
continue
|
| 36 |
+
if i % interval == 0:
|
| 37 |
+
frames.append(pil_img)
|
| 38 |
+
video.release()
|
| 39 |
+
return frames
|
| 40 |
+
except:
|
| 41 |
+
frames=[]
|
| 42 |
+
return frames
|
| 43 |
+
|
| 44 |
+
def llava(user_prompt, history):
|
| 45 |
+
image = user_prompt["files"][-1]
|
| 46 |
+
txt = user_prompt["text"]
|
| 47 |
+
img = user_prompt["files"]
|
| 48 |
+
|
| 49 |
+
video_extensions = ("avi", "mp4", "mov", "mkv", "flv", "wmv", "mjpeg", "wav", "gif", "webm", "m4v", "3gp")
|
| 50 |
+
image_extensions = Image.registered_extensions()
|
| 51 |
+
image_extensions = tuple([ex for ex, f in image_extensions.items()])
|
| 52 |
+
|
| 53 |
+
if image.endswith(video_extensions):
|
| 54 |
+
image = sample_frames(image)
|
| 55 |
+
image_tokens = "<image>" * int(len(image))
|
| 56 |
+
prompt = f"<|im_start|>user {image_tokens}\n{user_prompt}<|im_end|><|im_start|>assistant"
|
| 57 |
+
|
| 58 |
+
elif image.endswith(image_extensions):
|
| 59 |
+
image = Image.open(image).convert("RGB")
|
| 60 |
+
prompt = f"<|im_start|>user <image>\n{user_prompt}<|im_end|><|im_start|>assistant"
|
| 61 |
+
|
| 62 |
+
print(len(image))
|
| 63 |
+
|
| 64 |
+
inputs = processor(prompt, image, return_tensors="pt")
|
| 65 |
+
streamer = TextIteratorStreamer(processor, skip_prompt=True, **{"skip_special_tokens": True})
|
| 66 |
+
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=1024)
|
| 67 |
+
generated_text = ""
|
| 68 |
+
|
| 69 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 70 |
+
thread.start()
|
| 71 |
|
| 72 |
+
buffer = ""
|
| 73 |
+
for new_text in streamer:
|
| 74 |
+
buffer += new_text
|
| 75 |
+
yield buffer
|
| 76 |
|
| 77 |
def extract_text_from_webpage(html_content):
|
| 78 |
soup = BeautifulSoup(html_content, 'html.parser')
|
|
|
|
| 122 |
|
| 123 |
# Handle image processing
|
| 124 |
if message["files"]:
|
| 125 |
+
llava(message, history)
|
| 126 |
+
break
|
| 127 |
+
|
|
|
|
| 128 |
|
| 129 |
# Define function metadata for user interface
|
| 130 |
functions_metadata = [
|