prithivMLmods commited on
Commit
460a5cb
·
verified ·
1 Parent(s): 7837e17

update app

Browse files
Files changed (1) hide show
  1. app.py +99 -63
app.py CHANGED
@@ -6,6 +6,7 @@ import requests
6
  import time
7
  import asyncio
8
  from threading import Thread
 
9
 
10
  import gradio as gr
11
  import spaces
@@ -21,9 +22,75 @@ from transformers import (
21
  AutoTokenizer,
22
  TextIteratorStreamer,
23
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  # Constants for text generation
26
- MAX_MAX_NEW_TOKENS = 2048
27
  DEFAULT_MAX_NEW_TOKENS = 1024
28
  MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
29
 
@@ -84,7 +151,7 @@ def downsample_video(video_path):
84
  total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
85
  fps = vidcap.get(cv2.CAP_PROP_FPS)
86
  frames = []
87
- frame_indices = np.linspace(0, total_frames - 1, 10, dtype=int)
88
  for i in frame_indices:
89
  vidcap.set(cv2.CAP_PROP_POS_FRAMES, i)
90
  success, image = vidcap.read()
@@ -108,20 +175,15 @@ def generate_image(model_name: str, text: str, image: Image.Image,
108
  Yields raw text and Markdown-formatted text.
109
  """
110
  if model_name == "SkyCaptioner-V1":
111
- processor = processor_m
112
- model = model_m
113
  elif model_name == "DeepCaption-VLA-7B":
114
- processor = processor_n
115
- model = model_n
116
  elif model_name == "SpaceThinker-3B":
117
- processor = processor_z
118
- model = model_z
119
  elif model_name == "coreOCR-7B-050325-preview":
120
- processor = processor_k
121
- model = model_k
122
  elif model_name == "SpaceOm-3B":
123
- processor = processor_y
124
- model = model_y
125
  else:
126
  yield "Invalid model selected.", "Invalid model selected."
127
  return
@@ -133,7 +195,7 @@ def generate_image(model_name: str, text: str, image: Image.Image,
133
  messages = [{
134
  "role": "user",
135
  "content": [
136
- {"type": "image", "image": image},
137
  {"type": "text", "text": text},
138
  ]
139
  }]
@@ -143,7 +205,7 @@ def generate_image(model_name: str, text: str, image: Image.Image,
143
  images=[image],
144
  return_tensors="pt",
145
  padding=True,
146
- truncation=False,
147
  max_length=MAX_INPUT_TOKEN_LENGTH
148
  ).to(device)
149
  streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
@@ -169,20 +231,15 @@ def generate_video(model_name: str, text: str, video_path: str,
169
  Yields raw text and Markdown-formatted text.
170
  """
171
  if model_name == "SkyCaptioner-V1":
172
- processor = processor_m
173
- model = model_m
174
  elif model_name == "DeepCaption-VLA-7B":
175
- processor = processor_n
176
- model = model_n
177
  elif model_name == "SpaceThinker-3B":
178
- processor = processor_z
179
- model = model_z
180
  elif model_name == "coreOCR-7B-050325-preview":
181
- processor = processor_k
182
- model = model_k
183
  elif model_name == "SpaceOm-3B":
184
- processor = processor_y
185
- model = model_y
186
  else:
187
  yield "Invalid model selected.", "Invalid model selected."
188
  return
@@ -206,7 +263,7 @@ def generate_video(model_name: str, text: str, video_path: str,
206
  add_generation_prompt=True,
207
  return_dict=True,
208
  return_tensors="pt",
209
- truncation=False,
210
  max_length=MAX_INPUT_TOKEN_LENGTH
211
  ).to(device)
212
  streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
@@ -244,67 +301,46 @@ video_examples = [
244
  ]
245
 
246
  css = """
247
- .submit-btn {
248
- background-color: #2980b9 !important;
249
- color: white !important;
250
- }
251
- .submit-btn:hover {
252
- background-color: #3498db !important;
253
  }
254
- .canvas-output {
255
- border: 2px solid #4682B4;
256
- border-radius: 10px;
257
- padding: 20px;
258
  }
259
  """
260
 
261
  # Create the Gradio Interface
262
- with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
263
- gr.Markdown("# **[VisionScope R2](https://huggingface.co/collections/prithivMLmods/multimodal-implementations-67c9982ea04b39f0608badb0)**")
264
  with gr.Row():
265
- with gr.Column():
266
  with gr.Tabs():
267
  with gr.TabItem("Image Inference"):
268
  image_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
269
  image_upload = gr.Image(type="pil", label="Image", height=290)
270
- image_submit = gr.Button("Submit", elem_classes="submit-btn")
271
- gr.Examples(
272
- examples=image_examples,
273
- inputs=[image_query, image_upload]
274
- )
275
  with gr.TabItem("Video Inference"):
276
  video_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
277
  video_upload = gr.Video(label="Video", height=290)
278
- video_submit = gr.Button("Submit", elem_classes="submit-btn")
279
- gr.Examples(
280
- examples=video_examples,
281
- inputs=[video_query, video_upload]
282
- )
283
  with gr.Accordion("Advanced options", open=False):
284
  max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
285
  temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=4.0, step=0.1, value=0.6)
286
  top_p = gr.Slider(label="Top-p (nucleus sampling)", minimum=0.05, maximum=1.0, step=0.05, value=0.9)
287
  top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
288
  repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
289
- with gr.Column():
290
- with gr.Column(elem_classes="canvas-output"):
291
- gr.Markdown("## Output")
292
- output = gr.Textbox(label="Raw Output Stream", interactive=False, lines=5, show_copy_button=True)
293
- with gr.Accordion("(Result.md)", open=False):
294
- markdown_output = gr.Markdown(label="Formatted Result")
295
  model_choice = gr.Radio(
296
  choices=["DeepCaption-VLA-7B", "SkyCaptioner-V1", "SpaceThinker-3B", "coreOCR-7B-050325-preview", "SpaceOm-3B"],
297
  label="Select Model",
298
  value="DeepCaption-VLA-7B"
299
  )
300
- gr.Markdown("**Model Info 💻** | [Report Bug](https://huggingface.co/spaces/prithivMLmods/VisionScope-R2/discussions)")
301
- gr.Markdown("> [SkyCaptioner-V1](https://huggingface.co/Skywork/SkyCaptioner-V1): structural video captioning model designed to generate high-quality, structural descriptions for video data. It integrates specialized sub-expert models.")
302
- gr.Markdown("> [SpaceThinker-Qwen2.5VL-3B](https://huggingface.co/remyxai/SpaceThinker-Qwen2.5VL-3B): thinking/reasoning multimodal/vision-language model (VLM) trained to enhance spatial reasoning.")
303
- gr.Markdown("> [coreOCR-7B-050325-preview](https://huggingface.co/prithivMLmods/coreOCR-7B-050325-preview): model is a fine-tuned version of qwen/qwen2-vl-7b, optimized for document-level optical character recognition (ocr), long-context vision-language understanding.")
304
- gr.Markdown("> [SpaceOm](https://huggingface.co/remyxai/SpaceOm): SpaceOm, the reasoning traces in the spacethinker dataset average ~200 thinking tokens, so now included longer reasoning traces in the training data to help the model use more tokens in reasoning.")
305
- gr.Markdown("> [DeepCaption-VLA-7B](https://huggingface.co/prithivMLmods/DeepCaption-VLA-7B): DeepCaption-VLA-7B model is a fine-tuned version of Qwen2.5-VL-7B-Instruct, tailored for Image Captioning and VLA. This variant is designed to generate precise, highly descriptive captions.")
306
- gr.Markdown(">⚠️note: all the models in space are not guaranteed to perform well in video inference use cases.")
307
-
308
  image_submit.click(
309
  fn=generate_image,
310
  inputs=[model_choice, image_query, image_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
@@ -317,4 +353,4 @@ with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
317
  )
318
 
319
  if __name__ == "__main__":
320
- demo.queue(max_size=30).launch(share=True, mcp_server=True, ssr_mode=False, show_error=True)
 
6
  import time
7
  import asyncio
8
  from threading import Thread
9
+ from typing import Iterable
10
 
11
  import gradio as gr
12
  import spaces
 
22
  AutoTokenizer,
23
  TextIteratorStreamer,
24
  )
25
+ from gradio.themes import Soft
26
+ from gradio.themes.utils import colors, fonts, sizes
27
+
28
+ # --- Theme and CSS Definition ---
29
+
30
+ colors.steel_blue = colors.Color(
31
+ name="steel_blue",
32
+ c50="#EBF3F8",
33
+ c100="#D3E5F0",
34
+ c200="#A8CCE1",
35
+ c300="#7DB3D2",
36
+ c400="#529AC3",
37
+ c500="#4682B4", # SteelBlue base color
38
+ c600="#3E72A0",
39
+ c700="#36638C",
40
+ c800="#2E5378",
41
+ c900="#264364",
42
+ c950="#1E3450",
43
+ )
44
+
45
+ class SteelBlueTheme(Soft):
46
+ def __init__(
47
+ self,
48
+ *,
49
+ primary_hue: colors.Color | str = colors.gray,
50
+ secondary_hue: colors.Color | str = colors.steel_blue,
51
+ neutral_hue: colors.Color | str = colors.slate,
52
+ text_size: sizes.Size | str = sizes.text_lg,
53
+ font: fonts.Font | str | Iterable[fonts.Font | str] = (
54
+ fonts.GoogleFont("Outfit"), "Arial", "sans-serif",
55
+ ),
56
+ font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
57
+ fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
58
+ ),
59
+ ):
60
+ super().__init__(
61
+ primary_hue=primary_hue,
62
+ secondary_hue=secondary_hue,
63
+ neutral_hue=neutral_hue,
64
+ text_size=text_size,
65
+ font=font,
66
+ font_mono=font_mono,
67
+ )
68
+ super().set(
69
+ background_fill_primary="*primary_50",
70
+ background_fill_primary_dark="*primary_900",
71
+ body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
72
+ body_background_fill_dark="linear-gradient(135deg, *primary_900, *primary_800)",
73
+ button_primary_text_color="white",
74
+ button_primary_text_color_hover="white",
75
+ button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
76
+ button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
77
+ button_primary_background_fill_dark="linear-gradient(90deg, *secondary_600, *secondary_800)",
78
+ button_primary_background_fill_hover_dark="linear-gradient(90deg, *secondary_500, *secondary_500)",
79
+ slider_color="*secondary_500",
80
+ slider_color_dark="*secondary_600",
81
+ block_title_text_weight="600",
82
+ block_border_width="3px",
83
+ block_shadow="*shadow_drop_lg",
84
+ button_primary_shadow="*shadow_drop_lg",
85
+ button_large_padding="11px",
86
+ color_accent_soft="*primary_100",
87
+ block_label_background_fill="*primary_200",
88
+ )
89
+
90
+ steel_blue_theme = SteelBlueTheme()
91
 
92
  # Constants for text generation
93
+ MAX_MAX_NEW_TOKENS = 4096
94
  DEFAULT_MAX_NEW_TOKENS = 1024
95
  MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
96
 
 
151
  total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
152
  fps = vidcap.get(cv2.CAP_PROP_FPS)
153
  frames = []
154
+ frame_indices = np.linspace(0, total_frames - 1, min(total_frames, 10), dtype=int)
155
  for i in frame_indices:
156
  vidcap.set(cv2.CAP_PROP_POS_FRAMES, i)
157
  success, image = vidcap.read()
 
175
  Yields raw text and Markdown-formatted text.
176
  """
177
  if model_name == "SkyCaptioner-V1":
178
+ processor, model = processor_m, model_m
 
179
  elif model_name == "DeepCaption-VLA-7B":
180
+ processor, model = processor_n, model_n
 
181
  elif model_name == "SpaceThinker-3B":
182
+ processor, model = processor_z, model_z
 
183
  elif model_name == "coreOCR-7B-050325-preview":
184
+ processor, model = processor_k, model_k
 
185
  elif model_name == "SpaceOm-3B":
186
+ processor, model = processor_y, model_y
 
187
  else:
188
  yield "Invalid model selected.", "Invalid model selected."
189
  return
 
195
  messages = [{
196
  "role": "user",
197
  "content": [
198
+ {"type": "image"},
199
  {"type": "text", "text": text},
200
  ]
201
  }]
 
205
  images=[image],
206
  return_tensors="pt",
207
  padding=True,
208
+ truncation=True,
209
  max_length=MAX_INPUT_TOKEN_LENGTH
210
  ).to(device)
211
  streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
 
231
  Yields raw text and Markdown-formatted text.
232
  """
233
  if model_name == "SkyCaptioner-V1":
234
+ processor, model = processor_m, model_m
 
235
  elif model_name == "DeepCaption-VLA-7B":
236
+ processor, model = processor_n, model_n
 
237
  elif model_name == "SpaceThinker-3B":
238
+ processor, model = processor_z, model_z
 
239
  elif model_name == "coreOCR-7B-050325-preview":
240
+ processor, model = processor_k, model_k
 
241
  elif model_name == "SpaceOm-3B":
242
+ processor, model = processor_y, model_y
 
243
  else:
244
  yield "Invalid model selected.", "Invalid model selected."
245
  return
 
263
  add_generation_prompt=True,
264
  return_dict=True,
265
  return_tensors="pt",
266
+ truncation=True,
267
  max_length=MAX_INPUT_TOKEN_LENGTH
268
  ).to(device)
269
  streamer = TextIteratorStreamer(processor, skip_prompt=True, skip_special_tokens=True)
 
301
  ]
302
 
303
  css = """
304
+ #main-title h1 {
305
+ font-size: 2.3em !important;
 
 
 
 
306
  }
307
+ #output-title h2 {
308
+ font-size: 2.1em !important;
 
 
309
  }
310
  """
311
 
312
  # Create the Gradio Interface
313
+ with gr.Blocks(css=css, theme=steel_blue_theme) as demo:
314
+ gr.Markdown("# **VisionScope R2**", elem_id="main-title")
315
  with gr.Row():
316
+ with gr.Column(scale=2):
317
  with gr.Tabs():
318
  with gr.TabItem("Image Inference"):
319
  image_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
320
  image_upload = gr.Image(type="pil", label="Image", height=290)
321
+ image_submit = gr.Button("Submit", variant="primary")
322
+ gr.Examples(examples=image_examples, inputs=[image_query, image_upload])
 
 
 
323
  with gr.TabItem("Video Inference"):
324
  video_query = gr.Textbox(label="Query Input", placeholder="Enter your query here...")
325
  video_upload = gr.Video(label="Video", height=290)
326
+ video_submit = gr.Button("Submit", variant="primary")
327
+ gr.Examples(examples=video_examples, inputs=[video_query, video_upload])
 
 
 
328
  with gr.Accordion("Advanced options", open=False):
329
  max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=MAX_MAX_NEW_TOKENS, step=1, value=DEFAULT_MAX_NEW_TOKENS)
330
  temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=4.0, step=0.1, value=0.6)
331
  top_p = gr.Slider(label="Top-p (nucleus sampling)", minimum=0.05, maximum=1.0, step=0.05, value=0.9)
332
  top_k = gr.Slider(label="Top-k", minimum=1, maximum=1000, step=1, value=50)
333
  repetition_penalty = gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.2)
334
+ with gr.Column(scale=3):
335
+ gr.Markdown("## Output", elem_id="output-title")
336
+ output = gr.Textbox(label="Raw Output Stream", interactive=False, lines=14, show_copy_button=True)
337
+ with gr.Accordion("(Result.md)", open=False):
338
+ markdown_output = gr.Markdown(label="Formatted Result")
 
339
  model_choice = gr.Radio(
340
  choices=["DeepCaption-VLA-7B", "SkyCaptioner-V1", "SpaceThinker-3B", "coreOCR-7B-050325-preview", "SpaceOm-3B"],
341
  label="Select Model",
342
  value="DeepCaption-VLA-7B"
343
  )
 
 
 
 
 
 
 
 
344
  image_submit.click(
345
  fn=generate_image,
346
  inputs=[model_choice, image_query, image_upload, max_new_tokens, temperature, top_p, top_k, repetition_penalty],
 
353
  )
354
 
355
  if __name__ == "__main__":
356
+ demo.queue(max_size=50).launch(mcp_server=True, ssr_mode=False, show_error=True)