akhaliq HF staff commited on
Commit
be6090c
1 Parent(s): 5174661

add input image component

Browse files
Files changed (1) hide show
  1. app.py +23 -17
app.py CHANGED
@@ -19,20 +19,26 @@ model = Magma.from_checkpoint(
19
  device = 'cuda:0'
20
  )
21
 
22
- def generate(context, length, temperature, top_k):
23
- context = context.strip()
24
 
25
- url_regex = r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)'
26
- lines = context.split('\n')
27
- inputs = []
28
- for line in lines:
29
- if re.match(url_regex, line):
30
- try:
31
- inputs.append(ImageInput(line))
32
- except Exception as e:
33
- return str(e)
34
- else:
35
- inputs.append(line)
 
 
 
 
 
 
36
 
37
  ## returns a tensor of shape: (1, 149, 4096)
38
  embeddings = model.preprocess_inputs(inputs)
@@ -50,10 +56,10 @@ def generate(context, length, temperature, top_k):
50
  iface = gr.Interface(
51
  fn=generate,
52
  inputs=[
53
- gr.inputs.Textbox(
54
- label="Prompt (image URLs need to be on their own lines):",
55
- default="https://www.art-prints-on-demand.com/kunst/thomas_cole/woods_hi.jpg\nDescribe the painting:",
56
- lines=7),
57
  gr.inputs.Slider(minimum=1, maximum=100, default=15, step=1, label="Output tokens:"),
58
  gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.7, label='Temperature'),
59
  gr.inputs.Slider(minimum=0, maximum=100, default=0, step=1, label='Top K')
 
19
  device = 'cuda:0'
20
  )
21
 
22
+ def generate(image,context, length, temperature, top_k):
23
+ # context = context.strip()
24
 
25
+ # url_regex = r'https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)'
26
+ # lines = context.split('\n')
27
+ # inputs = []
28
+ # for line in lines:
29
+ # if re.match(url_regex, line):
30
+ # try:
31
+ # inputs.append(ImageInput(line))
32
+ # except Exception as e:
33
+ # return str(e)
34
+ # else:
35
+ # inputs.append(line)
36
+
37
+ inputs =[
38
+ ## supports urls and path/to/image
39
+ ImageInput(image),
40
+ context
41
+ ]
42
 
43
  ## returns a tensor of shape: (1, 149, 4096)
44
  embeddings = model.preprocess_inputs(inputs)
 
56
  iface = gr.Interface(
57
  fn=generate,
58
  inputs=[
59
+ [gr.inputs.Image(type="filepath"),gr.inputs.Textbox(
60
+ label="Prompt:",
61
+ default="Describe the painting:",
62
+ lines=7)],
63
  gr.inputs.Slider(minimum=1, maximum=100, default=15, step=1, label="Output tokens:"),
64
  gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.7, label='Temperature'),
65
  gr.inputs.Slider(minimum=0, maximum=100, default=0, step=1, label='Top K')