florafeng commited on
Commit
a02ac6f
·
verified ·
1 Parent(s): e12fb24

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -33
app.py CHANGED
@@ -1,50 +1,43 @@
1
  import gradio as gr
2
- import requests
3
- from PIL import Image
4
- from io import BytesIO
5
 
6
- API_URL = "https://YOUR-BACKEND-SPACE.hf.space/run/predict"
 
7
 
 
8
 
9
- def generate(prompt, seed):
10
- response = requests.post(
11
- API_URL,
12
- json={
13
- "data": [prompt, int(seed)]
14
- }
15
- )
16
-
17
- if response.status_code != 200:
18
- return f"Error: {response.status_code} - {response.text}"
19
-
20
- data = response.json()
21
-
22
- result = data["data"][0]
23
 
24
- # If backend returns a PIL image serialized as URL/path
25
- if isinstance(result, str) and result.startswith("http"):
26
- img_bytes = requests.get(result).content
27
- image = Image.open(BytesIO(img_bytes))
28
- else:
29
- # fallback (Gradio usually handles this automatically)
 
30
  return result
31
 
32
- return image
 
33
 
34
 
35
  with gr.Blocks() as demo:
36
  gr.Markdown("# 🎨 AI Bedroom Generator")
37
 
38
- prompt = gr.Textbox(
39
- label="Prompt",
40
- value="a unique bedroom with dolls, pillows, paintings"
41
- )
42
-
43
- seed = gr.Number(value=123, label="Seed")
44
 
45
  btn = gr.Button("Generate")
46
- output = gr.Image()
47
 
48
- btn.click(generate, inputs=[prompt, seed], outputs=output)
 
 
 
 
 
 
49
 
50
  demo.launch()
 
1
  import gradio as gr
2
+ from gradio_client import Client
 
 
3
 
4
+ # 🔴 Replace with your backend Space URL
5
+ BACKEND_URL = "https://florafeng-uclip-img-gen.hf.space"
6
 
7
+ client = Client(BACKEND_URL)
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
+ def generate(prompt, seed):
11
+ try:
12
+ result = client.predict(
13
+ prompt,
14
+ seed,
15
+ api_name="/predict" # default for gr.Interface
16
+ )
17
  return result
18
 
19
+ except Exception as e:
20
+ return f"Error: {str(e)}"
21
 
22
 
23
  with gr.Blocks() as demo:
24
  gr.Markdown("# 🎨 AI Bedroom Generator")
25
 
26
+ with gr.Row():
27
+ prompt = gr.Textbox(
28
+ label="Prompt",
29
+ value="a unique bedroom with dolls, pillows, paintings"
30
+ )
31
+ seed = gr.Number(value=123, label="Seed")
32
 
33
  btn = gr.Button("Generate")
 
34
 
35
+ output = gr.Image(label="Generated Image")
36
+
37
+ btn.click(
38
+ fn=generate,
39
+ inputs=[prompt, seed],
40
+ outputs=output
41
+ )
42
 
43
  demo.launch()