glarr commited on
Commit
2144a09
·
0 Parent(s):
Files changed (3) hide show
  1. README.md +13 -0
  2. app.py +69 -0
  3. requirements.txt +4 -0
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Prompt Reverse Engineer
3
+ emoji: 🔁
4
+ colorFrom: gray
5
+ colorTo: blue
6
+ sdk: gradio
7
+ sdk_version: 6.31.0
8
+ python_version: "3.12.12"
9
+ app_file: app.py
10
+ pinned: false
11
+ models:
12
+ - Qwen/Qwen3-VL-8B-Instruct
13
+ ---
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import torch
4
+ from transformers import AutoProcessor, Qwen3VLForConditionalGeneration
5
+
6
+ MODEL_ID = "Qwen/Qwen3-VL-8B-Instruct"
7
+ PROMPT = "Write an imperative AI prompt that thoroughly describes the image provided. The prompt makes a text-to-image model generate an exact replica of the source image without being provided the source image. Prompt consists of 4 sentences."
8
+
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ model = Qwen3VLForConditionalGeneration.from_pretrained(MODEL_ID, dtype="auto").to(device)
11
+ processor = AutoProcessor.from_pretrained(MODEL_ID)
12
+
13
+
14
+ @spaces.GPU
15
+ def generate(image, max_new_tokens):
16
+ if image is None:
17
+ raise gr.Error("Upload an image.")
18
+
19
+ messages = [
20
+ {
21
+ "role": "user",
22
+ "content": [
23
+ {"type": "image", "image": image},
24
+ {"type": "text", "text": PROMPT},
25
+ ],
26
+ }
27
+ ]
28
+
29
+ inputs = processor.apply_chat_template(
30
+ messages,
31
+ tokenize=True,
32
+ add_generation_prompt=True,
33
+ return_dict=True,
34
+ return_tensors="pt"
35
+ )
36
+ inputs = inputs.to(model.device)
37
+
38
+ # Inference: Generation of the output
39
+ generated_ids = model.generate(**inputs, max_new_tokens=int(max_new_tokens))
40
+ generated_ids_trimmed = [
41
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
42
+ ]
43
+ output_text = processor.batch_decode(
44
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
45
+ )
46
+ return output_text[0]
47
+
48
+
49
+ with gr.Blocks(title="Prompt Reverse Engineer") as demo:
50
+ gr.Markdown("# Prompt Reverse Engineer")
51
+ gr.Markdown("## Image to Prompt")
52
+ gr.Markdown("Turn an image into a text-to-image prompt with Qwen3-VL.")
53
+
54
+ with gr.Row():
55
+ with gr.Column():
56
+ image = gr.Image(type="pil", label="Image")
57
+ max_tokens = gr.Slider(32, 1024, value=214, step=1, label="Max Tokens")
58
+ generate_button = gr.Button("Generate", variant="primary")
59
+
60
+ with gr.Column():
61
+ generated_text = gr.Textbox(
62
+ label="Generated Text", lines=12, interactive=False, buttons=["copy"]
63
+ )
64
+ gr.Markdown("Author: Larrain, G")
65
+
66
+ generate_button.click(generate, [image, max_tokens], generated_text)
67
+
68
+ if __name__ == "__main__":
69
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ spaces
2
+ torch==2.13.0
3
+ torchvision==0.28.0
4
+ transformers==5.22.1