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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -24
app.py CHANGED
@@ -2,11 +2,11 @@ import gradio as gr
2
  import requests
3
  import json
4
  import io
 
5
  from PIL import Image
6
  import base64
7
  from prance import ResolvingParser
8
 
9
-
10
  SCHEMA_URL = "http://localhost:5000/openapi.json"
11
  FILENAME = "openapi.json"
12
  schema_response = requests.get(SCHEMA_URL)
@@ -18,55 +18,83 @@ with open(FILENAME, "wb") as f:
18
  f.write(r.content)
19
 
20
  parser = ResolvingParser(FILENAME)
 
21
  print(parser.specification)
22
 
23
-
24
  def extract_property_info(prop):
25
- # Handle 'allOf' by merging all contained properties (assuming simple case of enum merging)
26
- if "allOf" in prop:
27
- combined_prop = {}
28
- for subprop in prop["allOf"]:
29
- combined_prop.update(subprop)
30
- prop = combined_prop
31
- return prop
32
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  def create_gradio_app(api_spec, api_url):
35
  inputs = []
36
- input_schema = api_spec["components"]["schemas"]["PredictionRequest"]["properties"][
37
- "input"
38
- ]["properties"]
39
 
40
  for name, prop in input_schema.items():
 
41
  prop = extract_property_info(
42
  prop
43
  ) # Extract property info correctly for 'allOf'
44
  print(prop)
45
  if "enum" in prop:
46
  input_field = gr.Dropdown(
47
- choices=prop["enum"], label=name, value=prop.get("default")
48
  )
49
  elif prop["type"] == "integer":
50
- input_field = gr.Number(
51
- label=name,
 
52
  value=prop.get("default"),
53
  minimum=prop.get("minimum"),
54
  maximum=prop.get("maximum"),
55
  step=1,
56
  )
57
  elif prop["type"] == "number":
58
- input_field = gr.Number(
59
- label=name,
 
60
  value=prop.get("default"),
61
  minimum=prop.get("minimum"),
62
  maximum=prop.get("maximum"),
63
  )
64
  elif prop["type"] == "boolean":
65
- input_field = gr.Checkbox(label=name, value=prop.get("default"))
 
 
 
 
66
  elif prop["type"] == "string" and prop.get("format") == "uri":
67
- input_field = gr.File(label=name)
 
 
68
  else: # Assuming string type for simplicity, can add more types as needed
69
- input_field = gr.Textbox(label=name, value=prop.get("default"))
 
 
 
70
  inputs.append(input_field)
71
 
72
  def predict(**kwargs):
@@ -102,9 +130,6 @@ def create_gradio_app(api_spec, api_url):
102
  return gr.Interface(fn=predict, inputs=inputs, outputs=output_component)
103
 
104
 
105
- # Use the modified function with the API URL
106
- api_spec = parser.specification
107
-
108
  API_URL = "http://localhost:5000/predictions"
109
  app = create_gradio_app(api_spec, API_URL)
110
  app.launch()
 
2
  import requests
3
  import json
4
  import io
5
+ import os
6
  from PIL import Image
7
  import base64
8
  from prance import ResolvingParser
9
 
 
10
  SCHEMA_URL = "http://localhost:5000/openapi.json"
11
  FILENAME = "openapi.json"
12
  schema_response = requests.get(SCHEMA_URL)
 
18
  f.write(r.content)
19
 
20
  parser = ResolvingParser(FILENAME)
21
+ 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):
 
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()