Spaces:
Running
on
Zero
Running
on
Zero
update
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import argparse
|
2 |
import json
|
3 |
import numpy as np
|
@@ -8,9 +9,17 @@ from openai import OpenAI
|
|
8 |
from func_timeout import FunctionTimedOut, func_timeout
|
9 |
from tqdm import tqdm
|
10 |
|
11 |
-
|
|
|
12 |
TEST_FOLDER = "c4f5"
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
INPUT_STRUCTION_TEMPLATE = """Here is a gray scale images representing with integer values 0-9.
|
15 |
{image_str}
|
16 |
Please write a Python program that generates the image using our own custom turtle module"""
|
@@ -208,25 +217,39 @@ def generate_grid_images(folder):
|
|
208 |
return image_array
|
209 |
|
210 |
|
|
|
211 |
def llm_call(question_prompt, model_name,
|
212 |
temperature=1, max_tokens=320,
|
213 |
top_p=1, n_samples=64, stop=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
214 |
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
|
|
|
|
|
|
|
|
|
|
228 |
|
229 |
-
|
230 |
|
231 |
|
232 |
import cv2
|
@@ -287,12 +310,9 @@ turtle.save('{fname}')
|
|
287 |
def run(img_str):
|
288 |
prompt = PROMPT_TEMPLATE.format(input_struction=INPUT_STRUCTION_TEMPLATE.format(image_str=img_str))
|
289 |
if not MOCK:
|
290 |
-
|
291 |
-
print(
|
292 |
-
codes =
|
293 |
-
for i, choice in enumerate(response.choices):
|
294 |
-
print(f"Choice {i}: {choice.text}")
|
295 |
-
codes.append(choice.text)
|
296 |
else:
|
297 |
codes = MOCK_RESPONSE
|
298 |
|
@@ -396,7 +416,7 @@ def main():
|
|
396 |
gr.Markdown("""Here we can draw a target image using the sketchpad, and see what kinds of graphics program LLM generates. To allow the LLM to visually perceive the input image, we convert the image to ASCII strings.""")
|
397 |
gr.Markdown("## Draw logo")
|
398 |
with gr.Column():
|
399 |
-
canvas = gr.Sketchpad(canvas_size=(512,512), brush=Brush(colors=["black"], default_size=
|
400 |
submit_button = gr.Button("Submit")
|
401 |
output_image = gr.Image(label="output")
|
402 |
|
|
|
1 |
+
import spaces
|
2 |
import argparse
|
3 |
import json
|
4 |
import numpy as np
|
|
|
9 |
from func_timeout import FunctionTimedOut, func_timeout
|
10 |
from tqdm import tqdm
|
11 |
|
12 |
+
HUGGINGFACE=True
|
13 |
+
MOCK = False
|
14 |
TEST_FOLDER = "c4f5"
|
15 |
|
16 |
+
if HUGGINGFACE:
|
17 |
+
MODEL_NAME="xu3kev/deepseekcoder-7b-logo-pbe"
|
18 |
+
import torch
|
19 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
20 |
+
hug_model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=torch.float16, attn_implementation="flash_attention_2",).to('cuda')
|
21 |
+
hug_tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
22 |
+
|
23 |
INPUT_STRUCTION_TEMPLATE = """Here is a gray scale images representing with integer values 0-9.
|
24 |
{image_str}
|
25 |
Please write a Python program that generates the image using our own custom turtle module"""
|
|
|
217 |
return image_array
|
218 |
|
219 |
|
220 |
+
@spaces.GPU
|
221 |
def llm_call(question_prompt, model_name,
|
222 |
temperature=1, max_tokens=320,
|
223 |
top_p=1, n_samples=64, stop=None):
|
224 |
+
if HUGGINGFACE:
|
225 |
+
model_inputs = hug_tokenizer([question_prompt], return_tensors="pt").to('cuda')
|
226 |
+
generated_ids = hug_model.generate(**model_inputs, max_length=1400, temperature=1, num_return_sequences=16, do_sample=True)
|
227 |
+
responses = hug_tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
|
228 |
+
codes = []
|
229 |
+
for response in responses:
|
230 |
+
codes.append(response[len(question_prompt):].strip()+'\n')
|
231 |
+
return codes
|
232 |
|
233 |
+
else:
|
234 |
+
client = OpenAI(base_url=f"http://localhost:{PORT}/v1", api_key="empty")
|
235 |
+
|
236 |
+
response = client.completions.create(
|
237 |
+
prompt=question_prompt,
|
238 |
+
model=model_name,
|
239 |
+
temperature=temperature,
|
240 |
+
max_tokens=max_tokens,
|
241 |
+
top_p=top_p,
|
242 |
+
frequency_penalty=0,
|
243 |
+
presence_penalty=0,
|
244 |
+
n=n_samples,
|
245 |
+
stop=stop
|
246 |
+
)
|
247 |
+
codes = []
|
248 |
+
for i, choice in enumerate(response.choices):
|
249 |
+
print(f"Choice {i}: {choice.text}")
|
250 |
+
codes.append(choice.text)
|
251 |
|
252 |
+
return codes
|
253 |
|
254 |
|
255 |
import cv2
|
|
|
310 |
def run(img_str):
|
311 |
prompt = PROMPT_TEMPLATE.format(input_struction=INPUT_STRUCTION_TEMPLATE.format(image_str=img_str))
|
312 |
if not MOCK:
|
313 |
+
responses = llm_call(prompt, MODEL_NAME)
|
314 |
+
print(responses)
|
315 |
+
codes = responses
|
|
|
|
|
|
|
316 |
else:
|
317 |
codes = MOCK_RESPONSE
|
318 |
|
|
|
416 |
gr.Markdown("""Here we can draw a target image using the sketchpad, and see what kinds of graphics program LLM generates. To allow the LLM to visually perceive the input image, we convert the image to ASCII strings.""")
|
417 |
gr.Markdown("## Draw logo")
|
418 |
with gr.Column():
|
419 |
+
canvas = gr.Sketchpad(canvas_size=(512,512), brush=Brush(colors=["black"], default_size=2, color_mode='fixed'))
|
420 |
submit_button = gr.Button("Submit")
|
421 |
output_image = gr.Image(label="output")
|
422 |
|