Spaces:
Runtime error
Runtime error
Commit
·
34de0ae
1
Parent(s):
f626979
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
-
import json
|
4 |
|
5 |
def get_code_screenshot(code, title, theme, background, darkMode, padding, language):
|
6 |
-
# Prepare the payload
|
7 |
payload = {
|
8 |
"code": code,
|
9 |
"title": title,
|
@@ -13,19 +24,14 @@ def get_code_screenshot(code, title, theme, background, darkMode, padding, langu
|
|
13 |
"padding": padding,
|
14 |
"language": language
|
15 |
}
|
16 |
-
# Send POST request to Rayso API
|
17 |
response = requests.post("https://rayso.herokuapp.com/api", json=payload)
|
18 |
-
# Check for a valid response
|
19 |
if response.status_code == 200:
|
20 |
-
# Convert response to json
|
21 |
data = response.json()
|
22 |
-
# Get image URL from response
|
23 |
image_url = data.get('url')
|
24 |
return image_url
|
25 |
else:
|
26 |
return f"Failed to get image: {response.status_code}"
|
27 |
|
28 |
-
# Define Gradio interface
|
29 |
iface = gr.Interface(
|
30 |
fn=get_code_screenshot,
|
31 |
inputs=[
|
@@ -37,9 +43,21 @@ iface = gr.Interface(
|
|
37 |
gr.inputs.Dropdown(choices=["16", "32", "64", "128"], label="Padding"),
|
38 |
gr.inputs.Textbox(default="auto", placeholder="Enter language..."),
|
39 |
],
|
40 |
-
outputs=[gr.outputs.Image(type="url")]
|
41 |
-
live=True
|
42 |
)
|
43 |
|
44 |
if __name__ == "__main__":
|
45 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Based on the information obtained from various sources, to optimize your Gradio app for deployment on Hugging Face Spaces, follow these steps:
|
2 |
+
|
3 |
+
1. **Create a New Space on Hugging Face**:
|
4 |
+
- Select Gradio as the SDK when creating a new space on Hugging Face. This will initialize your space with the latest version of Gradio. Set the `sdk` property to `gradio` in your `README.md` file's YAML block [oai_citation:1,Gradio Spaces](https://huggingface.co/docs/hub/spaces-sdks-gradio).
|
5 |
+
|
6 |
+
2. **Prepare Your Dependencies**:
|
7 |
+
- If your app requires additional dependencies, create a `requirements.txt` file in your repository, and list all necessary libraries there [oai_citation:2,Gradio Spaces](https://huggingface.co/docs/hub/spaces-sdks-gradio).
|
8 |
+
|
9 |
+
3. **Implement Your Gradio App**:
|
10 |
+
- Create a new file called `app.py` in your repository.
|
11 |
+
- Import necessary libraries, define your function to interact with the Rayso API, and create a Gradio interface.
|
12 |
+
- Below is a simplified version of your original script adjusted for Hugging Face Spaces:
|
13 |
+
```python
|
14 |
import gradio as gr
|
15 |
import requests
|
|
|
16 |
|
17 |
def get_code_screenshot(code, title, theme, background, darkMode, padding, language):
|
|
|
18 |
payload = {
|
19 |
"code": code,
|
20 |
"title": title,
|
|
|
24 |
"padding": padding,
|
25 |
"language": language
|
26 |
}
|
|
|
27 |
response = requests.post("https://rayso.herokuapp.com/api", json=payload)
|
|
|
28 |
if response.status_code == 200:
|
|
|
29 |
data = response.json()
|
|
|
30 |
image_url = data.get('url')
|
31 |
return image_url
|
32 |
else:
|
33 |
return f"Failed to get image: {response.status_code}"
|
34 |
|
|
|
35 |
iface = gr.Interface(
|
36 |
fn=get_code_screenshot,
|
37 |
inputs=[
|
|
|
43 |
gr.inputs.Dropdown(choices=["16", "32", "64", "128"], label="Padding"),
|
44 |
gr.inputs.Textbox(default="auto", placeholder="Enter language..."),
|
45 |
],
|
46 |
+
outputs=[gr.outputs.Image(type="url")]
|
|
|
47 |
)
|
48 |
|
49 |
if __name__ == "__main__":
|
50 |
+
iface.launch()
|
51 |
+
```
|
52 |
+
|
53 |
+
4. **Deploy Your Gradio App**:
|
54 |
+
- After creating your Gradio app, deploy it to Hugging Face Spaces by running the following command in your app directory:
|
55 |
+
```bash
|
56 |
+
gradio deploy
|
57 |
+
```
|
58 |
+
- This command gathers some basic metadata and then launches your app on Hugging Face Spaces [oai_citation:3,Sharing Your App - gradio.app](https://www.gradio.app/guides/sharing-your-app#:~:text=After%20you%20have%20created%20a,and%20then%20launch%20your%20app).
|
59 |
+
|
60 |
+
5. **Review and Test Your App**:
|
61 |
+
- Once deployed, test your app on Hugging Face Spaces to ensure it's working as expected. If necessary, make any required adjustments to your code, and redeploy the app.
|
62 |
+
|
63 |
+
Now your Gradio app should be optimized and deployed on Hugging Face Spaces, allowing users to interact with the Rayso API through a user-friendly interface.
|