Spaces:
Runtime error
Runtime error
Commit
·
9c6c4fb
0
Parent(s):
re-init
Browse files- .gitattributes +34 -0
- .gitignore +1 -0
- README.md +13 -0
- __pycache__/style.cpython-310.pyc +0 -0
- app.py +139 -0
- requirements.txt +3 -0
- style.py +39 -0
.gitattributes
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
env/
|
README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: REST API with Gradio and Huggingface Spaces
|
| 3 |
+
emoji: 👩💻
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.34.2
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: openrail
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
This is a demo from this article: https://www.tomsoderlund.com/ai/building-ai-powered-rest-api
|
__pycache__/style.cpython-310.pyc
ADDED
|
Binary file (726 Bytes). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import gradio.themes
|
| 3 |
+
import numpy as np
|
| 4 |
+
from style import js_func, css_func
|
| 5 |
+
import tempfile
|
| 6 |
+
import pandas as pd
|
| 7 |
+
|
| 8 |
+
def greet(name="Stranger", intensity=1, exclaim=False):
|
| 9 |
+
greeting = f"Hello, {name}{'!' * int(intensity)}"
|
| 10 |
+
if exclaim:
|
| 11 |
+
greeting = greeting.upper()
|
| 12 |
+
return greeting
|
| 13 |
+
|
| 14 |
+
def calculator(num1, operation, num2):
|
| 15 |
+
if operation == "add":
|
| 16 |
+
result = num1 + num2
|
| 17 |
+
elif operation == "subtract":
|
| 18 |
+
result = num1 - num2
|
| 19 |
+
elif operation == "multiply":
|
| 20 |
+
result = num1 * num2
|
| 21 |
+
elif operation == "divide":
|
| 22 |
+
if num2 == 0:
|
| 23 |
+
raise gr.Error("Cannot divide by zero!")
|
| 24 |
+
result = num1 / num2
|
| 25 |
+
return result
|
| 26 |
+
|
| 27 |
+
def sepia(input_img):
|
| 28 |
+
sepia_filter = np.array([
|
| 29 |
+
[0.393, 0.769, 0.189],
|
| 30 |
+
[0.349, 0.686, 0.168],
|
| 31 |
+
[0.272, 0.534, 0.131]
|
| 32 |
+
])
|
| 33 |
+
sepia_img = input_img @ sepia_filter.T
|
| 34 |
+
sepia_img = np.clip(sepia_img, 0, 255).astype(np.uint8)
|
| 35 |
+
return sepia_img
|
| 36 |
+
|
| 37 |
+
def download_text(text):
|
| 38 |
+
if not text:
|
| 39 |
+
text = ""
|
| 40 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as f:
|
| 41 |
+
f.write(text)
|
| 42 |
+
return f.name
|
| 43 |
+
|
| 44 |
+
def download_csv(result):
|
| 45 |
+
if result is None:
|
| 46 |
+
result = ""
|
| 47 |
+
df = pd.DataFrame({"Result": [result]})
|
| 48 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".csv", mode="w", encoding="utf-8") as f:
|
| 49 |
+
df.to_csv(f, index=False)
|
| 50 |
+
return f.name
|
| 51 |
+
|
| 52 |
+
theme = gr.Theme.from_hub("LPX55/modal_ai")
|
| 53 |
+
|
| 54 |
+
with gr.Blocks(theme=theme, js=js_func, css=css_func) as demo:
|
| 55 |
+
with gr.Sidebar(width="25vw"):
|
| 56 |
+
gr.Image("logo.png", width=80, show_label=False)
|
| 57 |
+
gr.Markdown("""
|
| 58 |
+
# 🤖 Gradio API Demo
|
| 59 |
+
_Build, test, and share AI tools instantly!_
|
| 60 |
+
|
| 61 |
+
---
|
| 62 |
+
**Navigation**
|
| 63 |
+
- [Greeting](#greeting)
|
| 64 |
+
- [Calculator](#calculator)
|
| 65 |
+
- [Sepia Image](#sepia-image)
|
| 66 |
+
---
|
| 67 |
+
[GitHub Repo](https://github.com/yourrepo) | [Docs](https://yourdocs)
|
| 68 |
+
---
|
| 69 |
+
**Tips:**
|
| 70 |
+
- Try the examples in each tab!
|
| 71 |
+
- Download your results with the button on the right.
|
| 72 |
+
---
|
| 73 |
+
|
| 74 |
+
_v1.0.0 | Powered by Gradio_
|
| 75 |
+
""")
|
| 76 |
+
gr.Code(
|
| 77 |
+
"""from gradio_client import Client
|
| 78 |
+
client = Client("YOUR_URL")
|
| 79 |
+
print(client.predict("Alex", 5, False, api_name="/greet"))
|
| 80 |
+
|
| 81 |
+
""", language="python", label="**API Example:**"
|
| 82 |
+
)
|
| 83 |
+
gr.Button("Reset All", elem_id="reset-btn")
|
| 84 |
+
|
| 85 |
+
with gr.Tab("Greeting"):
|
| 86 |
+
gr.Markdown("## Greeting Generator")
|
| 87 |
+
with gr.Row():
|
| 88 |
+
with gr.Column():
|
| 89 |
+
name = gr.Textbox(label="Name", info="Enter your name", placeholder="e.g. Alex")
|
| 90 |
+
intensity = gr.Slider(1, 20, value=3, step=1, label="Intensity", info="How excited should the greeting be?")
|
| 91 |
+
with gr.Accordion("Advanced Options", open=False):
|
| 92 |
+
exclaim = gr.Checkbox(label="Shout (all caps)", info="Make the greeting all uppercase and add exclamations")
|
| 93 |
+
greet_btn = gr.Button("Greet")
|
| 94 |
+
with gr.Column():
|
| 95 |
+
greet_output = gr.Textbox(label="Greeting", lines=2)
|
| 96 |
+
download_greet_btn = gr.DownloadButton(label="Download Greeting", value=download_text, inputs=greet_output)
|
| 97 |
+
gr.Examples(
|
| 98 |
+
[["Jill", 1, False], ["Sam", 3, True], ["Alex", 5, False]],
|
| 99 |
+
inputs=[name, intensity, exclaim],
|
| 100 |
+
outputs=greet_output,
|
| 101 |
+
fn=greet
|
| 102 |
+
)
|
| 103 |
+
greet_btn.click(greet, [name, intensity, exclaim], greet_output)
|
| 104 |
+
|
| 105 |
+
with gr.Tab("Calculator"):
|
| 106 |
+
gr.Markdown("## Toy Calculator")
|
| 107 |
+
with gr.Row():
|
| 108 |
+
with gr.Column():
|
| 109 |
+
num1 = gr.Number(label="Number 1", info="First number")
|
| 110 |
+
operation = gr.Radio(["add", "subtract", "multiply", "divide"], label="Operation", info="Choose the operation")
|
| 111 |
+
num2 = gr.Number(label="Number 2", info="Second number")
|
| 112 |
+
calc_btn = gr.Button("Calculate")
|
| 113 |
+
with gr.Column():
|
| 114 |
+
calc_output = gr.Number(label="Result")
|
| 115 |
+
download_calc_btn = gr.DownloadButton(label="Download Result", value=download_csv, inputs=calc_output)
|
| 116 |
+
gr.Examples(
|
| 117 |
+
[[45, "add", 3], [3.14, "divide", 2], [144, "multiply", 2.5], [0, "subtract", 1.2]],
|
| 118 |
+
inputs=[num1, operation, num2],
|
| 119 |
+
outputs=calc_output,
|
| 120 |
+
fn=calculator
|
| 121 |
+
)
|
| 122 |
+
calc_btn.click(calculator, [num1, operation, num2], calc_output)
|
| 123 |
+
|
| 124 |
+
with gr.Tab("Sepia Image"):
|
| 125 |
+
gr.Markdown("## Sepia Image Filter")
|
| 126 |
+
image_input = gr.Image(label="Input Image", type="numpy")
|
| 127 |
+
sepia_btn = gr.Button("Apply Sepia")
|
| 128 |
+
image_output = gr.Image(label="Sepia Image")
|
| 129 |
+
sepia_btn.click(sepia, image_input, image_output)
|
| 130 |
+
|
| 131 |
+
gr.Markdown("""
|
| 132 |
+
---
|
| 133 |
+
## 📡 API Usage
|
| 134 |
+
- Every function in this demo is automatically available as a REST API!
|
| 135 |
+
- View the [OpenAPI schema](./openapi.json) or click "Use via API" in the footer.
|
| 136 |
+
- Try the [gradio_client](https://www.gradio.app/guides/getting-started-with-the-python-client) or [@gradio/client](https://www.gradio.app/guides/getting-started-with-the-js-client) to call these endpoints programmatically.
|
| 137 |
+
""")
|
| 138 |
+
|
| 139 |
+
demo.launch(mcp_server=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio[mcp]
|
| 2 |
+
numpy
|
| 3 |
+
pandas
|
style.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
js_func = """
|
| 2 |
+
function refresh() {
|
| 3 |
+
const url = new URL(window.location);
|
| 4 |
+
|
| 5 |
+
if (url.searchParams.get('__theme') !== 'dark') {
|
| 6 |
+
url.searchParams.set('__theme', 'dark');
|
| 7 |
+
window.location.href = url.href;
|
| 8 |
+
}
|
| 9 |
+
}
|
| 10 |
+
"""
|
| 11 |
+
|
| 12 |
+
css_func = """
|
| 13 |
+
.sidebar{
|
| 14 |
+
background-color: (--block-border-color) !important;
|
| 15 |
+
border-right: #373737 !important;
|
| 16 |
+
border-right-width: 3px !important;
|
| 17 |
+
border-right-style: solid !important;
|
| 18 |
+
}
|
| 19 |
+
.post{
|
| 20 |
+
color: #1c1a1a !important;
|
| 21 |
+
font-size: 0.8rem !important;
|
| 22 |
+
padding: 4px 2px !important;
|
| 23 |
+
}
|
| 24 |
+
.highlight{
|
| 25 |
+
color: #1c1a1a !important;
|
| 26 |
+
font-size: 0.8rem !important;
|
| 27 |
+
padding: 4px 2px !important;
|
| 28 |
+
}
|
| 29 |
+
.prose a{
|
| 30 |
+
color: #aba9a9 !important;
|
| 31 |
+
}
|
| 32 |
+
.prose a:visited{
|
| 33 |
+
color: #6b6767 !important;
|
| 34 |
+
}
|
| 35 |
+
.prose hr {
|
| 36 |
+
margin-top: 2em !important;
|
| 37 |
+
margin-bottom: 2em !important;
|
| 38 |
+
|
| 39 |
+
"""
|