akhaliq HF staff commited on
Commit
82ee039
1 Parent(s): de4d170

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -23
app.py CHANGED
@@ -5,6 +5,7 @@ import base64
5
  from io import BytesIO
6
  from PIL import Image
7
  import numpy as np
 
8
 
9
  # Initialize the Together client
10
  api_key = os.environ.get('TOGETHER_API_KEY')
@@ -31,24 +32,13 @@ def generate_gradio_app(image):
31
  img_str = base64.b64encode(buffered.getvalue()).decode()
32
 
33
  # Prepare the messages for the API call
34
- system_message = """You are an AI assistant that can analyze wireframe images and generate detailed Gradio code based on their content. Your task is to provide a complete, runnable Gradio application that recreates the UI elements seen in the wireframe."""
35
-
36
  user_message = f"""
37
  <image>
38
  data:image/png;base64,{img_str}
39
  </image>
40
 
41
- Analyze this wireframe image and generate a complete Python code using Gradio that recreates all the main elements seen in the image. Follow these guidelines:
42
- 1. Use appropriate Gradio components that best represent each UI element in the wireframe.
43
- 2. Include all necessary imports at the beginning of the code.
44
- 3. Implement placeholder functions for any interactive elements (buttons, inputs, etc.).
45
- 4. Use gr.Blocks() to create a layout that matches the wireframe as closely as possible.
46
- 5. Add appropriate labels and descriptions for all components.
47
- 6. Include the gr.Blocks().launch() call at the end of the code.
48
- 7. Provide a complete, runnable Gradio application that can be executed as-is.
49
- 8. Add comments explaining the purpose of each major section or component.
50
-
51
- Please generate the entire code, including all necessary parts to make it a fully functional Gradio application.
52
  """
53
 
54
  messages = [
@@ -60,24 +50,29 @@ def generate_gradio_app(image):
60
  response = client.chat.completions.create(
61
  model="meta-llama/Llama-Vision-Free",
62
  messages=messages,
63
- max_tokens=2048,
64
  temperature=0.7,
65
  top_p=0.7,
66
  top_k=50,
67
  repetition_penalty=1,
68
- stop=["<|im_end|>"],
69
- stream=True
70
  )
71
 
72
- # Collect the streamed response
73
- generated_code = ""
74
- for chunk in response:
75
- if chunk.choices[0].delta.content is not None:
76
- generated_code += chunk.choices[0].delta.content
77
-
78
  return generated_code
 
79
  except Exception as e:
80
- return f"An error occurred: {str(e)}\n\nPlease try again or check your API key and connection."
 
 
 
 
 
 
 
 
 
 
81
 
82
  with gr.Blocks() as demo:
83
  gr.Markdown("# Turn your wireframe into a Gradio app")
 
5
  from io import BytesIO
6
  from PIL import Image
7
  import numpy as np
8
+ import json
9
 
10
  # Initialize the Together client
11
  api_key = os.environ.get('TOGETHER_API_KEY')
 
32
  img_str = base64.b64encode(buffered.getvalue()).decode()
33
 
34
  # Prepare the messages for the API call
35
+ system_message = "You are an AI assistant that can analyze wireframe images and generate detailed Gradio code based on their content."
 
36
  user_message = f"""
37
  <image>
38
  data:image/png;base64,{img_str}
39
  </image>
40
 
41
+ Analyze this wireframe image and generate a complete Python code using Gradio that recreates all the main elements seen in the image. Provide a complete, runnable Gradio application.
 
 
 
 
 
 
 
 
 
 
42
  """
43
 
44
  messages = [
 
50
  response = client.chat.completions.create(
51
  model="meta-llama/Llama-Vision-Free",
52
  messages=messages,
53
+ max_tokens=5000,
54
  temperature=0.7,
55
  top_p=0.7,
56
  top_k=50,
57
  repetition_penalty=1,
 
 
58
  )
59
 
60
+ # Extract the generated code from the response
61
+ generated_code = response.choices[0].message.content
 
 
 
 
62
  return generated_code
63
+
64
  except Exception as e:
65
+ error_message = str(e)
66
+ if "400" in error_message:
67
+ return "Error 400: Bad Request. The API request was invalid. Please check your input and try again."
68
+ elif "401" in error_message:
69
+ return "Error 401: Unauthorized. Please check your API key and ensure it's correctly set."
70
+ elif "429" in error_message:
71
+ return "Error 429: Too Many Requests. Please wait a moment and try again."
72
+ elif "500" in error_message:
73
+ return "Error 500: Internal Server Error. There's an issue with the API service. Please try again later."
74
+ else:
75
+ return f"An unexpected error occurred: {error_message}\nPlease try again or contact support if the issue persists."
76
 
77
  with gr.Blocks() as demo:
78
  gr.Markdown("# Turn your wireframe into a Gradio app")