multimodalart HF Staff commited on
Commit
e807675
·
verified ·
1 Parent(s): 47ce9d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -56
app.py CHANGED
@@ -22,96 +22,80 @@ api_spec = parser.specification
22
  print(parser.specification)
23
 
24
  def extract_property_info(prop):
25
- # Initialize a dictionary to hold the combined properties
26
  combined_prop = {}
27
-
28
- # Identify the keywords to process. Extend this list if needed.
29
  merge_keywords = ["allOf", "anyOf", "oneOf"]
30
 
31
- # Loop through each keyword to check if it exists in the prop
32
  for keyword in merge_keywords:
33
  if keyword in prop:
34
  for subprop in prop[keyword]:
35
  combined_prop.update(subprop)
36
- # After merging, remove the keyword to avoid confusion
37
  del prop[keyword]
38
 
39
- # If no merge_keywords were found, copy the original property
40
  if not combined_prop:
41
  combined_prop = prop.copy()
42
 
43
- # Preserve specific properties defined outside of merge_keywords,
44
- # like 'description' and 'default', by updating them in combined_prop
45
  for key in ['description', 'default']:
46
  if key in prop:
47
  combined_prop[key] = prop[key]
48
 
49
- # This returns the combined property with preserved 'description' and 'default' values.
50
  return combined_prop
51
 
 
 
 
 
52
  def create_gradio_app(api_spec, api_url):
53
  inputs = []
 
54
  input_schema = api_spec["components"]["schemas"]["Input"]["properties"]
 
 
 
 
55
 
56
- for name, prop in input_schema.items():
57
- #print(prop)
58
- prop = extract_property_info(
59
- prop
60
- ) # Extract property info correctly for 'allOf'
61
- print(prop)
62
  if "enum" in prop:
63
  input_field = gr.Dropdown(
64
  choices=prop["enum"], label=prop.get("title"), info=prop.get("description"), value=prop.get("default")
65
  )
66
  elif prop["type"] == "integer":
67
  input_field = gr.Slider(
68
- label=prop.get("title"),
69
- info=prop.get("description"),
70
- value=prop.get("default"),
71
- minimum=prop.get("minimum"),
72
- maximum=prop.get("maximum"),
73
- step=1,
74
  )
75
  elif prop["type"] == "number":
76
  input_field = gr.Slider(
77
- label=prop.get("title"),
78
- info=prop.get("description"),
79
- value=prop.get("default"),
80
- minimum=prop.get("minimum"),
81
- maximum=prop.get("maximum"),
82
  )
83
  elif prop["type"] == "boolean":
84
- input_field = gr.Checkbox(
85
- label=prop.get("title"),
86
- info=prop.get("description"),
87
- value=prop.get("default")
88
- )
89
  elif prop["type"] == "string" and prop.get("format") == "uri":
90
- input_field = gr.File(
91
- label=prop.get("title"),
92
- )
93
- else: # Assuming string type for simplicity, can add more types as needed
94
- input_field = gr.Textbox(
95
- label=prop.get("title"),
96
- info=prop.get("description"),
97
- )
98
  inputs.append(input_field)
99
-
100
- def predict(**kwargs):
 
 
 
 
 
 
 
 
 
 
 
101
  payload = {"input": {}}
102
- for key, value in kwargs.items():
103
- if isinstance(
104
- value, io.BytesIO
105
- ): # For image inputs, convert to the desired format
106
- value.seek(0)
107
- value = (
108
- "data:image/jpeg;base64," + base64.b64encode(value.read()).decode()
109
- )
110
  payload["input"][key] = value
111
-
112
- response = requests.post(
113
- api_url, headers={"Content-Type": "application/json"}, json=payload
114
- )
115
  json_response = response.json()
116
 
117
  if "status" in json_response and json_response["status"] == "failed":
@@ -126,10 +110,9 @@ def create_gradio_app(api_spec, api_url):
126
 
127
  return output_images
128
 
129
- output_component = gr.Gallery(label="Output Images")
130
- return gr.Interface(fn=predict, inputs=inputs, outputs=output_component)
131
 
132
 
133
  API_URL = "http://localhost:5000/predictions"
134
  app = create_gradio_app(api_spec, API_URL)
135
- app.launch()
 
22
  print(parser.specification)
23
 
24
  def extract_property_info(prop):
 
25
  combined_prop = {}
 
 
26
  merge_keywords = ["allOf", "anyOf", "oneOf"]
27
 
 
28
  for keyword in merge_keywords:
29
  if keyword in prop:
30
  for subprop in prop[keyword]:
31
  combined_prop.update(subprop)
 
32
  del prop[keyword]
33
 
 
34
  if not combined_prop:
35
  combined_prop = prop.copy()
36
 
 
 
37
  for key in ['description', 'default']:
38
  if key in prop:
39
  combined_prop[key] = prop[key]
40
 
 
41
  return combined_prop
42
 
43
+ def sort_properties_by_order(properties):
44
+ ordered_properties = sorted(properties.items(), key=lambda x: x[1].get('x-order', float('inf')))
45
+ return ordered_properties
46
+
47
  def create_gradio_app(api_spec, api_url):
48
  inputs = []
49
+ outputs = []
50
  input_schema = api_spec["components"]["schemas"]["Input"]["properties"]
51
+ output_schema = api_spec["components"]["schemas"]["Output"]
52
+ ordered_input_schema = sort_properties_by_order(input_schema)
53
+ names = []
54
+ for name, prop in ordered_input_schema:
55
 
56
+ prop = extract_property_info(prop)
 
 
 
 
 
57
  if "enum" in prop:
58
  input_field = gr.Dropdown(
59
  choices=prop["enum"], label=prop.get("title"), info=prop.get("description"), value=prop.get("default")
60
  )
61
  elif prop["type"] == "integer":
62
  input_field = gr.Slider(
63
+ label=prop.get("title"), info=prop.get("description"), value=prop.get("default"),
64
+ minimum=prop.get("minimum"), maximum=prop.get("maximum"), step=1,
 
 
 
 
65
  )
66
  elif prop["type"] == "number":
67
  input_field = gr.Slider(
68
+ label=prop.get("title"), info=prop.get("description"), value=prop.get("default"),
69
+ minimum=prop.get("minimum"), maximum=prop.get("maximum"),
 
 
 
70
  )
71
  elif prop["type"] == "boolean":
72
+ input_field = gr.Checkbox(label=prop.get("title"), info=prop.get("description"), value=prop.get("default"))
 
 
 
 
73
  elif prop["type"] == "string" and prop.get("format") == "uri":
74
+ input_field = gr.File(label=prop.get("title"))
75
+ else:
76
+ input_field = gr.Textbox(label=prop.get("title"), info=prop.get("description"))
 
 
 
 
 
77
  inputs.append(input_field)
78
+ names.append(name)
79
+ print(names)
80
+ data_field = gr.State(value=names)
81
+ inputs.append(data_field)
82
+ if output_schema["type"] == "string":
83
+ output_component = gr.Gallery(label="Output Image")
84
+ outputs.append(output_component)
85
+ outputs.append(data_field)
86
+ #else if there's multiple outputs
87
+
88
+ def predict(*args):
89
+ print(args)
90
+ keys = args[-1]
91
  payload = {"input": {}}
92
+ for i, key in enumerate(keys):
93
+ value = args[i]
94
+ if(os.path.exists(value)):
95
+ value = "http://localhost:7860/file=" + value
 
 
 
 
96
  payload["input"][key] = value
97
+ print(payload)
98
+ response = requests.post(api_url, headers={"Content-Type": "application/json"}, json=payload)
 
 
99
  json_response = response.json()
100
 
101
  if "status" in json_response and json_response["status"] == "failed":
 
110
 
111
  return output_images
112
 
113
+ return gr.Interface(fn=predict, inputs=inputs, outputs=outputs if outputs else "label")
 
114
 
115
 
116
  API_URL = "http://localhost:5000/predictions"
117
  app = create_gradio_app(api_spec, API_URL)
118
+ app.launch(share=True)