multimodalart HF Staff commited on
Commit
2792f6f
·
verified ·
1 Parent(s): 9c90996

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -7
app.py CHANGED
@@ -44,6 +44,32 @@ def extract_property_info(prop):
44
  def sort_properties_by_order(properties):
45
  ordered_properties = sorted(properties.items(), key=lambda x: x[1].get('x-order', float('inf')))
46
  return ordered_properties
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
  def create_gradio_app(api_spec, api_url):
49
  inputs = []
@@ -110,18 +136,18 @@ def create_gradio_app(api_spec, api_url):
110
  print(json_response)
111
  if "status" in json_response and json_response["status"] == "failed":
112
  raise gr.Error("Failed to generate output")
 
 
113
 
114
- output_images = []
115
- for output_uri in json_response["output"]:
116
- if output_uri.startswith("data:image"):
117
  # Process as image
118
  base64_data = output_uri.split(",", 1)[1]
119
  image_data = base64.b64decode(base64_data)
120
  image_stream = io.BytesIO(image_data)
121
  image = Image.open(image_stream)
122
  return gr.update(visible=True, value=image), gr.update(visible=False), gr.update(visible=False), keys
123
- elif output_uri.startswith("data:audio"):
124
- # Process as audio
125
  base64_data = output_uri.split(",", 1)[1]
126
  audio_data = base64.b64decode(base64_data)
127
  audio_stream = io.BytesIO(audio_data)
@@ -129,8 +155,9 @@ def create_gradio_app(api_spec, api_url):
129
  filename = f"{uuid.uuid4()}.wav" # Change format as needed
130
  with open(filename, "wb") as audio_file:
131
  audio_file.write(audio_stream.getbuffer())
132
- return gr.update(visible=False), gr.update(visible=True, value=filename), gr.update(visible=False), keys
133
-
 
134
  else:
135
  raise gr.Error("The submission failed!")
136
  return gr.Interface(fn=predict, inputs=inputs, outputs=outputs)
 
44
  def sort_properties_by_order(properties):
45
  ordered_properties = sorted(properties.items(), key=lambda x: x[1].get('x-order', float('inf')))
46
  return ordered_properties
47
+
48
+
49
+ def parse_outputs(data):
50
+ values = []
51
+
52
+ if isinstance(data, dict):
53
+ # Handle case where data is an object
54
+ dict_values = []
55
+ for value in data.values():
56
+ extracted_values = parse_values(value)
57
+ # For dict, we append instead of extend to maintain list structure within objects
58
+ if isinstance(value, list):
59
+ dict_values += [extracted_values]
60
+ else:
61
+ dict_values += extracted_values
62
+ return dict_values
63
+ elif isinstance(data, list):
64
+ # Handle case where data is an array
65
+ list_values = []
66
+ for item in data:
67
+ # Here we extend to flatten the list since we're already in an array context
68
+ list_values += parse_values(item)
69
+ return list_values
70
+ else:
71
+ # Handle primitive data types directly
72
+ return [data]
73
 
74
  def create_gradio_app(api_spec, api_url):
75
  inputs = []
 
136
  print(json_response)
137
  if "status" in json_response and json_response["status"] == "failed":
138
  raise gr.Error("Failed to generate output")
139
+
140
+ outputs = parse_outputs(json_response)
141
 
142
+ for output in outputs:
143
+ if output.startswith("data:image"):
 
144
  # Process as image
145
  base64_data = output_uri.split(",", 1)[1]
146
  image_data = base64.b64decode(base64_data)
147
  image_stream = io.BytesIO(image_data)
148
  image = Image.open(image_stream)
149
  return gr.update(visible=True, value=image), gr.update(visible=False), gr.update(visible=False), keys
150
+ elif output.startswith("data:audio"):
 
151
  base64_data = output_uri.split(",", 1)[1]
152
  audio_data = base64.b64decode(base64_data)
153
  audio_stream = io.BytesIO(audio_data)
 
155
  filename = f"{uuid.uuid4()}.wav" # Change format as needed
156
  with open(filename, "wb") as audio_file:
157
  audio_file.write(audio_stream.getbuffer())
158
+ return gr.update(visible=False), gr.update(visible=True, value=filename), gr.update(visible=False), keys
159
+ else:
160
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True, value=output)
161
  else:
162
  raise gr.Error("The submission failed!")
163
  return gr.Interface(fn=predict, inputs=inputs, outputs=outputs)