akhaliq HF staff commited on
Commit
0a936ec
1 Parent(s): c190a55

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -0
app.py CHANGED
@@ -5,6 +5,7 @@ from io import BytesIO
5
  from PIL import Image
6
  import numpy as np
7
  import traceback
 
8
 
9
  def generate_gradio_app(api_key, image):
10
  if not api_key:
@@ -74,8 +75,22 @@ Please generate the Gradio code based on the provided image, focusing on the mos
74
  except Exception as e:
75
  error_message = str(e)
76
  stack_trace = traceback.format_exc()
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  return f"An error occurred: {error_message}\n\nStack trace:\n{stack_trace}\n\nPlease check your API key and try again."
78
 
 
79
  with gr.Blocks() as demo:
80
  gr.Markdown("# Generate Concise Gradio App from Wireframe")
81
  gr.Markdown("Enter your Together API key, upload an image of your UI design, and we'll generate a compact Gradio code to recreate its main elements.")
 
5
  from PIL import Image
6
  import numpy as np
7
  import traceback
8
+ import json
9
 
10
  def generate_gradio_app(api_key, image):
11
  if not api_key:
 
75
  except Exception as e:
76
  error_message = str(e)
77
  stack_trace = traceback.format_exc()
78
+
79
+ # Check if the error is related to the API response
80
+ if "TogetherErrorResponse" in error_message:
81
+ try:
82
+ # Try to parse the error message as JSON
83
+ error_data = json.loads(error_message.split("TogetherErrorResponse")[-1].strip())
84
+ if isinstance(error_data.get('code'), int):
85
+ error_data['code'] = str(error_data['code']) # Convert code to string
86
+ error_message = f"API Error: {error_data.get('message', 'Unknown error')}"
87
+ except json.JSONDecodeError:
88
+ # If parsing fails, use the original error message
89
+ pass
90
+
91
  return f"An error occurred: {error_message}\n\nStack trace:\n{stack_trace}\n\nPlease check your API key and try again."
92
 
93
+ # The rest of your code remains the same
94
  with gr.Blocks() as demo:
95
  gr.Markdown("# Generate Concise Gradio App from Wireframe")
96
  gr.Markdown("Enter your Together API key, upload an image of your UI design, and we'll generate a compact Gradio code to recreate its main elements.")