Spaces:
Sleeping
Sleeping
AI Backend Deploy commited on
Commit Β·
3959a6f
1
Parent(s): 6e9fffd
Fix: Use gr.Interface instead of Blocks to bypass Gradio schema bug
Browse files
app.py
CHANGED
|
@@ -10,6 +10,8 @@ import gradio as gr
|
|
| 10 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 11 |
from PIL import Image, ImageDraw
|
| 12 |
import numpy as np
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# ===== DEVICE CONFIGURATION =====
|
| 15 |
device = "cpu"
|
|
@@ -119,62 +121,66 @@ def image_fn(prompt, width, height):
|
|
| 119 |
b = int((np.sin((x + y) / 100 + seed * 0.7) * 127) + 128)
|
| 120 |
pixels[x, y] = (r, g, b)
|
| 121 |
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
| 123 |
except Exception as e:
|
| 124 |
-
return
|
| 125 |
|
| 126 |
|
| 127 |
# ===== GRADIO INTERFACE =====
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
| 178 |
|
| 179 |
|
| 180 |
# ===== INITIALIZE AND RUN =====
|
|
|
|
| 10 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 11 |
from PIL import Image, ImageDraw
|
| 12 |
import numpy as np
|
| 13 |
+
import base64
|
| 14 |
+
from io import BytesIO
|
| 15 |
|
| 16 |
# ===== DEVICE CONFIGURATION =====
|
| 17 |
device = "cpu"
|
|
|
|
| 121 |
b = int((np.sin((x + y) / 100 + seed * 0.7) * 127) + 128)
|
| 122 |
pixels[x, y] = (r, g, b)
|
| 123 |
|
| 124 |
+
buffered = BytesIO()
|
| 125 |
+
img.save(buffered, format="PNG")
|
| 126 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 127 |
+
return f"data:image/png;base64,{img_str}"
|
| 128 |
except Exception as e:
|
| 129 |
+
return f"Error: {str(e)}"
|
| 130 |
|
| 131 |
|
| 132 |
# ===== GRADIO INTERFACE =====
|
| 133 |
|
| 134 |
+
# Create individual interfaces
|
| 135 |
+
chat_demo = gr.Interface(
|
| 136 |
+
fn=chat_fn,
|
| 137 |
+
inputs=[
|
| 138 |
+
gr.Textbox(lines=3, label="Message"),
|
| 139 |
+
gr.Slider(50, 200, 150, step=10, label="Max Tokens"),
|
| 140 |
+
gr.Slider(0.1, 1.0, 0.7, step=0.1, label="Temperature")
|
| 141 |
+
],
|
| 142 |
+
outputs=gr.Textbox(lines=10, label="Response"),
|
| 143 |
+
title="π¬ Chat"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
code_demo = gr.Interface(
|
| 147 |
+
fn=code_fn,
|
| 148 |
+
inputs=[
|
| 149 |
+
gr.Textbox(lines=3, label="Description"),
|
| 150 |
+
gr.Slider(100, 300, 256, step=20, label="Max Tokens"),
|
| 151 |
+
gr.Slider(0.1, 1.0, 0.3, step=0.1, label="Temperature")
|
| 152 |
+
],
|
| 153 |
+
outputs=gr.Textbox(lines=10, label="Code"),
|
| 154 |
+
title="π» Code"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
summarize_demo = gr.Interface(
|
| 158 |
+
fn=summarize_fn,
|
| 159 |
+
inputs=[
|
| 160 |
+
gr.Textbox(lines=8, label="Text"),
|
| 161 |
+
gr.Slider(20, 150, 100, step=10, label="Summary Length")
|
| 162 |
+
],
|
| 163 |
+
outputs=gr.Textbox(lines=8, label="Summary"),
|
| 164 |
+
title="π Summarize"
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
image_demo = gr.Interface(
|
| 168 |
+
fn=image_fn,
|
| 169 |
+
inputs=[
|
| 170 |
+
gr.Textbox(label="Description"),
|
| 171 |
+
gr.Slider(128, 256, 256, step=32, label="Width"),
|
| 172 |
+
gr.Slider(128, 256, 256, step=32, label="Height")
|
| 173 |
+
],
|
| 174 |
+
outputs=gr.Textbox(label="Image (Base64)"),
|
| 175 |
+
title="π¨ Image"
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
# Create tabbed interface
|
| 179 |
+
demo = gr.TabbedInterface(
|
| 180 |
+
[chat_demo, code_demo, summarize_demo, image_demo],
|
| 181 |
+
tab_names=["π¬ Chat", "π» Code", "π Summarize", "π¨ Image"],
|
| 182 |
+
title="π€ Lightweight AI Backend"
|
| 183 |
+
)
|
| 184 |
|
| 185 |
|
| 186 |
# ===== INITIALIZE AND RUN =====
|