mrbeliever commited on
Commit
27d875e
Β·
verified Β·
1 Parent(s): 3901da8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -21
app.py CHANGED
@@ -16,12 +16,16 @@ processors = {
16
 
17
  default_question = "You are an image to prompt converter. Your work is to observe each and every detail of the image and craft a detailed prompt under 100 words in this format: [image content/subject, description of action, state, and mood], [art form, style], [artist/photographer reference if needed], [additional settings such as camera and lens settings, lighting, colors, effects, texture, background, rendering]."
18
 
 
 
 
 
19
  @spaces.GPU
20
  def run_example(image, text_input=default_question, model_id="microsoft/Phi-3.5-vision-instruct"):
21
  model = models[model_id]
22
  processor = processors[model_id]
23
 
24
- prompt = f"<|user|>\n<|image_1|>\n{text_input}<|end|>\n<|assistant|>\n"
25
  image = Image.fromarray(image).convert("RGB")
26
 
27
  inputs = processor(prompt, image, return_tensors="pt").to("cuda:0")
@@ -36,42 +40,53 @@ def run_example(image, text_input=default_question, model_id="microsoft/Phi-3.5-
36
  return response
37
 
38
  css = """
39
- .container {
40
  border: 2px solid #333;
41
  padding: 20px;
42
  max-width: 400px;
43
  margin: auto;
44
  }
45
  #input_img, #output_text {
46
- border: 2px solid #333;
47
- width: 100%;
48
- height: 300px;
49
- object-fit: cover;
50
  }
51
- .gr-button {
52
- width: 100%;
53
- margin-top: 10px;
54
  }
55
- #copy_button {
56
- float: right;
57
- margin-top: -30px;
 
 
 
 
 
 
 
 
 
58
  cursor: pointer;
 
59
  }
60
  """
61
 
62
  with gr.Blocks(css=css) as demo:
63
  with gr.Box(elem_id="container"):
64
- input_img = gr.Image(label="Input Picture", elem_id="input_img", type="pil")
65
- generate_button = gr.Button("Generate Prompt", elem_id="generate_button")
66
- with gr.Row():
67
- output_text = gr.Textbox(label="Output Text", elem_id="output_text", interactive=False)
68
- copy_button = gr.Button("Copy", elem_id="copy_button")
 
69
 
70
- # Copy functionality
71
- copy_button.click(fn=lambda text: text, inputs=output_text, outputs=None)
 
 
72
 
73
- # Generate functionality
74
- generate_button.click(run_example, [input_img, default_question], [output_text])
75
 
76
  demo.queue(api_open=False)
77
  demo.launch(debug=True, show_api=False)
 
16
 
17
  default_question = "You are an image to prompt converter. Your work is to observe each and every detail of the image and craft a detailed prompt under 100 words in this format: [image content/subject, description of action, state, and mood], [art form, style], [artist/photographer reference if needed], [additional settings such as camera and lens settings, lighting, colors, effects, texture, background, rendering]."
18
 
19
+ user_prompt = '<|user|>\n'
20
+ assistant_prompt = '<|assistant|>\n'
21
+ prompt_suffix = "<|end|>\n"
22
+
23
  @spaces.GPU
24
  def run_example(image, text_input=default_question, model_id="microsoft/Phi-3.5-vision-instruct"):
25
  model = models[model_id]
26
  processor = processors[model_id]
27
 
28
+ prompt = f"{user_prompt}<|image_1|>\n{text_input}{prompt_suffix}{assistant_prompt}"
29
  image = Image.fromarray(image).convert("RGB")
30
 
31
  inputs = processor(prompt, image, return_tensors="pt").to("cuda:0")
 
40
  return response
41
 
42
  css = """
43
+ #container {
44
  border: 2px solid #333;
45
  padding: 20px;
46
  max-width: 400px;
47
  margin: auto;
48
  }
49
  #input_img, #output_text {
50
+ border: 1px solid #444;
51
+ border-radius: 5px;
 
 
52
  }
53
+ #input_img {
54
+ height: 200px;
55
+ overflow: hidden;
56
  }
57
+ #output_text {
58
+ height: 150px;
59
+ overflow-y: auto;
60
+ }
61
+ .copy-btn {
62
+ display: inline-block;
63
+ padding: 5px 10px;
64
+ font-size: 14px;
65
+ background-color: #333;
66
+ color: #fff;
67
+ border: none;
68
+ border-radius: 3px;
69
  cursor: pointer;
70
+ margin-top: 10px;
71
  }
72
  """
73
 
74
  with gr.Blocks(css=css) as demo:
75
  with gr.Box(elem_id="container"):
76
+ input_img = gr.Image(label="Input Picture", elem_id="input_img")
77
+ text_input = gr.Textbox(value=default_question, visible=False)
78
+ submit_btn = gr.Button(value="Generate")
79
+ output_text = gr.Textbox(label="Output Text", elem_id="output_text")
80
+
81
+ submit_btn.click(run_example, [input_img, text_input], [output_text])
82
 
83
+ def copy_to_clipboard(content):
84
+ import pyperclip
85
+ pyperclip.copy(content)
86
+ return "Text copied!"
87
 
88
+ copy_button = gr.Button("Copy Text", elem_id="copy-btn")
89
+ copy_button.click(copy_to_clipboard, inputs=output_text, outputs=None)
90
 
91
  demo.queue(api_open=False)
92
  demo.launch(debug=True, show_api=False)