benjamin-paine commited on
Commit
de1fa08
Β·
verified Β·
1 Parent(s): 37cb1ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -6
app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  import spaces
4
  import torch
5
 
 
6
  from huggingface_hub import hf_hub_download
7
  from llama_cpp import Llama
8
 
@@ -12,14 +13,20 @@ model = hf_hub_download(
12
  )
13
  llama = None
14
 
15
- def trim_upsampled_caption(prompt):
 
 
 
16
  try:
17
  return re.match(r".*[\"β€œ](.*)[\"”]", prompt).group(1)
18
  except:
19
  return prompt
20
 
21
  @spaces.GPU
22
- def upsample(prompt):
 
 
 
23
  global llama
24
  if llama is None:
25
  llama = Llama(
@@ -34,12 +41,12 @@ def upsample(prompt):
34
  )
35
  response = completion["choices"][0]["message"]["content"]
36
  trimmed = trim_upsampled_caption(response)
37
- return response, trimmed
38
 
39
  demo = gr.Interface(
40
  title="NVIDIA Cosmos 🌌 Prompt Upsampler",
41
  description="""Upsample prompts using NVIDIA's 12B Cosmos model, based on Mistral NeMo 12B. This space uses llama.cpp with the Q8-0 quantized GGUF checkpoint.
42
-
43
  [[cosmos]](https://huggingface.co/nvidia/Cosmos-1.0-Prompt-Upsampler-12B-Text2World) [[transformers]](https://huggingface.co/appmana/Cosmos-1.0-Prompt-Upsampler-12B-Text2World-hf) [[gguf]](https://huggingface.co/mradermacher/Cosmos-1.0-Prompt-Upsampler-12B-Text2World-hf-GGUF)""",
44
  fn=upsample,
45
  inputs=gr.Text(
@@ -50,13 +57,16 @@ demo = gr.Interface(
50
  gr.Text(
51
  label="Trimmed Upsampled Prompt",
52
  interactive=False,
53
- value="In a sun-drenched backyard, a playful golden retriever bounds joyfully across the lush green grass, its tail wagging with excitement. The dog, adorned with a vibrant blue collar, is captivated by a bright yellow ball, which it cradles gently in its mouth, its eyes sparkling with anticipation. The camera, held steady, captures the dog's animated expressions as it trots towards the viewer, its movements fluid and graceful. The scene is bathed in warm, golden-hour light, enhancing the vibrant colors of the dog's fur and the lush surroundings. As the dog playfully tosses the ball into the air, it leaps with enthusiasm, showcasing its agility and playful spirit. The serene backdrop of a white fence and a distant tree line frames this heartwarming moment, inviting viewers to share in the simple joy of a dog's playful antics."
 
54
  ),
55
  gr.Text(
56
  label="Raw Upsampled Prompt",
57
  interactive=False,
58
- value="β€œIn a sun-drenched backyard, a playful golden retriever bounds joyfully across the lush green grass, its tail wagging with excitement. The dog, adorned with a vibrant blue collar, is captivated by a bright yellow ball, which it cradles gently in its mouth, its eyes sparkling with anticipation. The camera, held steady, captures the dog's animated expressions as it trots towards the viewer, its movements fluid and graceful. The scene is bathed in warm, golden-hour light, enhancing the vibrant colors of the dog's fur and the lush surroundings. As the dog playfully tosses the ball into the air, it leaps with enthusiasm, showcasing its agility and playful spirit. The serene backdrop of a white fence and a distant tree line frames this heartwarming moment, inviting viewers to share in the simple joy of a dog's playful antics.”"
 
59
  )
60
  ]
61
  )
 
62
  demo.launch()
 
3
  import spaces
4
  import torch
5
 
6
+ from typing import Tuple
7
  from huggingface_hub import hf_hub_download
8
  from llama_cpp import Llama
9
 
 
13
  )
14
  llama = None
15
 
16
+ def trim_upsampled_caption(prompt: str) -> str:
17
+ """
18
+ Tries to remove extra content around the caption. Cosmos tends to put the output caption in quotes.
19
+ """
20
  try:
21
  return re.match(r".*[\"β€œ](.*)[\"”]", prompt).group(1)
22
  except:
23
  return prompt
24
 
25
  @spaces.GPU
26
+ def upsample(prompt: str) -> Tuple[str, str]:
27
+ """
28
+ Instantiate llama instance if necessary and run prediction.
29
+ """
30
  global llama
31
  if llama is None:
32
  llama = Llama(
 
41
  )
42
  response = completion["choices"][0]["message"]["content"]
43
  trimmed = trim_upsampled_caption(response)
44
+ return trimmed, response
45
 
46
  demo = gr.Interface(
47
  title="NVIDIA Cosmos 🌌 Prompt Upsampler",
48
  description="""Upsample prompts using NVIDIA's 12B Cosmos model, based on Mistral NeMo 12B. This space uses llama.cpp with the Q8-0 quantized GGUF checkpoint.
49
+
50
  [[cosmos]](https://huggingface.co/nvidia/Cosmos-1.0-Prompt-Upsampler-12B-Text2World) [[transformers]](https://huggingface.co/appmana/Cosmos-1.0-Prompt-Upsampler-12B-Text2World-hf) [[gguf]](https://huggingface.co/mradermacher/Cosmos-1.0-Prompt-Upsampler-12B-Text2World-hf-GGUF)""",
51
  fn=upsample,
52
  inputs=gr.Text(
 
57
  gr.Text(
58
  label="Trimmed Upsampled Prompt",
59
  interactive=False,
60
+ value="In a sun-drenched backyard, a playful golden retriever bounds joyfully across the lush green grass, its tail wagging with excitement. The dog, adorned with a vibrant blue collar, is captivated by a bright yellow ball, which it cradles gently in its mouth, its eyes sparkling with anticipation. The camera, held steady, captures the dog's animated expressions as it trots towards the viewer, its movements fluid and graceful. The scene is bathed in warm, golden-hour light, enhancing the vibrant colors of the dog's fur and the lush surroundings. As the dog playfully tosses the ball into the air, it leaps with enthusiasm, showcasing its agility and playful spirit. The serene backdrop of a white fence and a distant tree line frames this heartwarming moment, inviting viewers to share in the simple joy of a dog's playful antics.",
61
+ show_copy_button=True
62
  ),
63
  gr.Text(
64
  label="Raw Upsampled Prompt",
65
  interactive=False,
66
+ value="β€œIn a sun-drenched backyard, a playful golden retriever bounds joyfully across the lush green grass, its tail wagging with excitement. The dog, adorned with a vibrant blue collar, is captivated by a bright yellow ball, which it cradles gently in its mouth, its eyes sparkling with anticipation. The camera, held steady, captures the dog's animated expressions as it trots towards the viewer, its movements fluid and graceful. The scene is bathed in warm, golden-hour light, enhancing the vibrant colors of the dog's fur and the lush surroundings. As the dog playfully tosses the ball into the air, it leaps with enthusiasm, showcasing its agility and playful spirit. The serene backdrop of a white fence and a distant tree line frames this heartwarming moment, inviting viewers to share in the simple joy of a dog's playful antics.”",
67
+ show_copy_button=True
68
  )
69
  ]
70
  )
71
+
72
  demo.launch()