appsnprojectsstpl-tech commited on
Commit
350d24d
·
1 Parent(s): 7f04a79

Add OmniRoute Integration for Image Editing

Browse files
Files changed (2) hide show
  1. app.py +111 -29
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,6 +1,10 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import spaces
 
 
 
 
4
 
5
  @spaces.GPU
6
  def generate_image(prompt, hf_token, progress=gr.Progress(track_tqdm=True)):
@@ -18,7 +22,46 @@ def generate_image(prompt, hf_token, progress=gr.Progress(track_tqdm=True)):
18
 
19
  return image
20
 
21
- # UI
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  custom_theme = gr.themes.Soft(
23
  primary_hue="blue",
24
  secondary_hue="indigo",
@@ -29,41 +72,80 @@ with gr.Blocks() as demo:
29
  gr.Markdown(
30
  """
31
  # ⚡ FLUX.1 Image Studio (Grok Quality)
32
- Generate ultra-fast images from text using the real-time FLUX.1-schnell model via Hugging Face Serverless API.
33
- *No local GPU Required! Generates in the cloud.*
34
  """
35
  )
36
 
37
- with gr.Row():
38
- with gr.Column(scale=1):
39
- hf_token = gr.Textbox(
40
- label="🔑 Hugging Face Access Token",
41
- placeholder="hf_...",
42
- type="password",
43
- info="Paste your Hugging Face Token here"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  )
45
- prompt = gr.Textbox(
46
- label="✨ Prompt",
47
- lines=3,
48
- placeholder="e.g. A futuristic cyberpunk city at night...",
49
- autofocus=True
50
  )
51
 
52
- generate_btn = gr.Button("🎨 Generate Image", variant="primary", size="lg")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- with gr.Column(scale=1):
55
- output_image = gr.Image(label="Result", type="pil", interactive=False)
56
-
57
- generate_btn.click(
58
- fn=generate_image,
59
- inputs=[prompt, hf_token],
60
- outputs=[output_image]
61
- )
62
- prompt.submit(
63
- fn=generate_image,
64
- inputs=[prompt, hf_token],
65
- outputs=[output_image]
66
- )
67
 
68
  if __name__ == "__main__":
69
  demo.launch(theme=custom_theme)
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import spaces
4
+ from openai import OpenAI
5
+ import io
6
+ import requests
7
+ from PIL import Image
8
 
9
  @spaces.GPU
10
  def generate_image(prompt, hf_token, progress=gr.Progress(track_tqdm=True)):
 
22
 
23
  return image
24
 
25
+ def edit_image(image, prompt, omni_url, omni_key, omni_model, progress=gr.Progress(track_tqdm=True)):
26
+ if not omni_url or not omni_key:
27
+ raise gr.Error("Please provide both your OmniRoute URL and API Key!")
28
+ if not image:
29
+ raise gr.Error("Please upload an image to edit!")
30
+ if not prompt:
31
+ raise gr.Error("Please enter a prompt for editing!")
32
+
33
+ client = OpenAI(
34
+ base_url=omni_url.strip(),
35
+ api_key=omni_key.strip()
36
+ )
37
+
38
+ img_byte_arr = io.BytesIO()
39
+ image.save(img_byte_arr, format='PNG')
40
+ img_byte_arr = img_byte_arr.getvalue()
41
+
42
+ try:
43
+ response = client.images.edit(
44
+ image=img_byte_arr,
45
+ prompt=prompt,
46
+ model=omni_model.strip()
47
+ )
48
+
49
+ if response.data and len(response.data) > 0:
50
+ url = response.data[0].url
51
+ if url:
52
+ img_response = requests.get(url)
53
+ edited = Image.open(io.BytesIO(img_response.content))
54
+ return edited
55
+ elif response.data[0].b64_json:
56
+ import base64
57
+ img_bytes = base64.b64decode(response.data[0].b64_json)
58
+ edited = Image.open(io.BytesIO(img_bytes))
59
+ return edited
60
+
61
+ raise gr.Error("OmniRoute returned an empty response.")
62
+ except Exception as e:
63
+ raise gr.Error(f"OmniRoute API Error: {str(e)}")
64
+
65
  custom_theme = gr.themes.Soft(
66
  primary_hue="blue",
67
  secondary_hue="indigo",
 
72
  gr.Markdown(
73
  """
74
  # ⚡ FLUX.1 Image Studio (Grok Quality)
75
+ Generate ultra-fast images from text using the real-time FLUX.1-schnell model via Hugging Face Serverless API. *No local GPU Required!*
 
76
  """
77
  )
78
 
79
+ with gr.Tabs():
80
+ with gr.Tab("✨ Generate New Image"):
81
+ with gr.Row():
82
+ with gr.Column(scale=1):
83
+ hf_token = gr.Textbox(
84
+ label="🔑 Hugging Face Access Token",
85
+ placeholder="hf_...",
86
+ type="password",
87
+ info="Paste your Hugging Face Token here"
88
+ )
89
+ prompt = gr.Textbox(
90
+ label="✨ Prompt",
91
+ lines=3,
92
+ placeholder="e.g. A futuristic cyberpunk city at night...",
93
+ autofocus=True
94
+ )
95
+
96
+ generate_btn = gr.Button("🎨 Generate Image", variant="primary", size="lg")
97
+
98
+ with gr.Column(scale=1):
99
+ output_image = gr.Image(label="Result", type="pil", interactive=False)
100
+
101
+ generate_btn.click(
102
+ fn=generate_image,
103
+ inputs=[prompt, hf_token],
104
+ outputs=[output_image]
105
  )
106
+ prompt.submit(
107
+ fn=generate_image,
108
+ inputs=[prompt, hf_token],
109
+ outputs=[output_image]
 
110
  )
111
 
112
+ with gr.Tab("🖌️ Edit Existing Image (OmniRoute)"):
113
+ with gr.Row():
114
+ with gr.Column(scale=1):
115
+ with gr.Accordion("⚙️ OmniRoute Configuration", open=True):
116
+ omni_url = gr.Textbox(
117
+ label="OmniRoute Base URL",
118
+ value="http://localhost:20128/v1",
119
+ info="The endpoint URL of your OmniRoute gateway"
120
+ )
121
+ omni_key = gr.Textbox(
122
+ label="🔑 OmniRoute API Key",
123
+ placeholder="Enter your key...",
124
+ type="password"
125
+ )
126
+ omni_model = gr.Textbox(
127
+ label="Model ID",
128
+ value="black-forest-labs/FLUX.1-schnell",
129
+ info="The underlying model to route to"
130
+ )
131
+
132
+ input_image = gr.Image(label="Image to Edit", type="pil")
133
+ edit_prompt = gr.Textbox(
134
+ label="✨ Edit Prompt",
135
+ lines=2,
136
+ placeholder="e.g. change the sky to red..."
137
+ )
138
+
139
+ edit_btn = gr.Button("🖌️ Edit Image", variant="primary", size="lg")
140
+
141
+ with gr.Column(scale=1):
142
+ edit_output = gr.Image(label="Edited Result", type="pil", interactive=False)
143
 
144
+ edit_btn.click(
145
+ fn=edit_image,
146
+ inputs=[input_image, edit_prompt, omni_url, omni_key, omni_model],
147
+ outputs=[edit_output]
148
+ )
 
 
 
 
 
 
 
 
149
 
150
  if __name__ == "__main__":
151
  demo.launch(theme=custom_theme)
requirements.txt CHANGED
@@ -1,3 +1,5 @@
1
  gradio
2
  huggingface_hub
3
- spaces
 
 
 
1
  gradio
2
  huggingface_hub
3
+ spaces
4
+ openai
5
+ pillow