rayochoajr commited on
Commit
73b5a0d
·
1 Parent(s): 71518c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -43
app.py CHANGED
@@ -3,58 +3,25 @@ import requests
3
  from PIL import Image
4
  from io import BytesIO
5
 
6
- # Function to interact with the Rayso API
7
- def get_code_screenshot(code, title, theme, background, darkMode, padding, language):
8
- # Constructing the payload to be sent to the Rayso API
9
- payload = {
10
- "code": code,
11
- "title": title,
12
- "theme": theme,
13
- "background": background,
14
- "darkMode": darkMode,
15
- "padding": padding,
16
- "language": language
17
- }
18
- # Sending a POST request to the Rayso API
19
  response = requests.post("https://rayso.herokuapp.com/api", json=payload)
20
- # Checking the response status code
21
  if response.status_code == 200:
22
- # Parsing the JSON response to obtain the image URL
23
- data = response.json()
24
- image_url = data.get('url')
25
- # Sending a GET request to fetch the image from the obtained URL
26
- response = requests.get(image_url)
27
- # Checking the response status code
28
- if response.status_code == 200:
29
- # Converting the image bytes to a PIL Image object using BytesIO
30
- image = Image.open(BytesIO(response.content))
31
- # Returning the PIL Image object to be displayed by Gradio
32
  return image
33
  else:
34
- # Returning an error message if failed to fetch the image
35
- return f"Failed to fetch image: {response.status_code}"
36
  else:
37
- # Returning an error message if failed to get the image URL
38
  return f"Failed to get image: {response.status_code}"
39
 
40
- # Gradio Interface
41
  iface = gr.Interface(
42
- fn=get_code_screenshot, # Function to be called on user input
43
- inputs=[
44
- gr.Textbox(lines=10, placeholder="Enter your code here...", label="Code"),
45
- gr.Textbox(default="Untitled-1", placeholder="Enter title...", label="Title"),
46
- gr.Dropdown(choices=["breeze", "candy", "crimson", "falcon", "meadow", "midnight", "raindrop", "sunset"], label="Theme"),
47
- gr.Checkbox(default=True, label="Background"),
48
- gr.Checkbox(default=True, label="Dark Mode"),
49
- gr.Dropdown(choices=["16", "32", "64", "128"], label="Padding"),
50
- gr.Textbox(default="auto", placeholder="Enter language...", label="Language"),
51
- ],
52
- outputs=[
53
- gr.Image(label="Generated Image"), # Updated to handle the PIL Image object returned by get_code_screenshot
54
- ],
55
- live=False # Set to False to only call the function when the submit button is pressed
56
  )
57
 
58
- # Launch the Gradio interface
59
  if __name__ == "__main__":
60
  iface.launch()
 
3
  from PIL import Image
4
  from io import BytesIO
5
 
6
+ def get_code_screenshot(code):
7
+ payload = {"code": code}
 
 
 
 
 
 
 
 
 
 
 
8
  response = requests.post("https://rayso.herokuapp.com/api", json=payload)
 
9
  if response.status_code == 200:
10
+ image_url = response.json().get('url')
11
+ image_response = requests.get(image_url)
12
+ if image_response.status_code == 200:
13
+ image = Image.open(BytesIO(image_response.content))
 
 
 
 
 
 
14
  return image
15
  else:
16
+ return f"Failed to fetch image: {image_response.status_code}"
 
17
  else:
 
18
  return f"Failed to get image: {response.status_code}"
19
 
 
20
  iface = gr.Interface(
21
+ fn=get_code_screenshot,
22
+ inputs=gr.Textbox(lines=10, placeholder="Enter your code here...", label="Code"),
23
+ outputs=gr.Image(label="Generated Image")
 
 
 
 
 
 
 
 
 
 
 
24
  )
25
 
 
26
  if __name__ == "__main__":
27
  iface.launch()