drdudddd commited on
Commit
22505ff
Β·
verified Β·
1 Parent(s): a7de814

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -127
app.py CHANGED
@@ -1,143 +1,55 @@
1
  import gradio as gr
2
- from gradio_client import Client, handle_file
3
-
4
- # ────────────────────────────────────────────────
5
- # Konfiguration der drei Spaces
6
- # ────────────────────────────────────────────────
7
- spaces = {
8
- "Omni Image Editor": {
9
- "url": "https://selfit-camera-omni-image-editor.hf.space/",
10
- "api_name": "/text_to_image_interface",
11
- "inputs": ["prompt", "aspect_ratio"],
12
- "default": {"aspect_ratio": "16:9"},
13
- "needs_image": False,
14
- "note": "Text-to-Image – braucht nur Prompt"
15
- },
16
- "FireRed Image Edit": {
17
- "url": "https://prithivmlmods-firered-image-edit-1-0-fast.hf.space/",
18
- "api_name": "/infer",
19
- "inputs": ["images", "prompt", "steps"], # Changed 'image' to 'images' (plural)
20
- "default": {"steps": 28},
21
- "needs_image": True,
22
- "note": "Image-to-Image Edit – braucht Referenzbild + Prompt"
23
- },
24
- "Ana2": {
25
- "url": "https://galaxydude2-ana2.hf.space/",
26
- "api_name": "/predict",
27
- "inputs": ["image"],
28
- "default": {},
29
- "needs_image": True,
30
- "note": "Analyzer (Nudity/NSFW?) – braucht nur Bild, kein Prompt"
31
- }
32
- }
33
-
34
- # ────────────────────────────────────────────────
35
- # Hauptfunktion – je nach Space unterschiedlicher Aufruf
36
- # ────────────────────────────────────────────────
37
- def run_space(selected_space, prompt_text, image_input=None):
38
- if selected_space not in spaces:
39
- return None, "UngΓΌltiger Space ausgewΓ€hlt"
40
-
41
- cfg = spaces[selected_space]
42
- status = f"β†’ {selected_space} | "
43
-
44
- # PrΓΌfen ob Bild benΓΆtigt wird
45
- if cfg["needs_image"] and image_input is None:
46
- return None, status + "Dieser Space braucht ein Referenzbild"
47
 
 
 
48
  try:
49
- client = Client(cfg["url"])
50
-
51
- kwargs = cfg["default"].copy()
52
- args_for_predict = [] # This list will remain empty with the current logic
53
-
54
- # Prompt nur bei Spaces ΓΌbergeben, die ihn erwarten
55
- if "prompt" in cfg["inputs"] and prompt_text.strip():
56
- kwargs["prompt"] = prompt_text.strip()
57
-
58
- # Bild ΓΌbergeben, wenn nΓΆtig
59
- if cfg["needs_image"] and image_input:
60
- processed_image = handle_file(image_input)
61
- if selected_space == "Ana2":
62
- # For Ana2, pass the image as a keyword argument named 'input_image_paths'
63
- kwargs["input_image_paths"] = processed_image
64
- elif selected_space == "FireRed Image Edit":
65
- # For FireRed, pass a list of dictionaries with 'image' key
66
- kwargs["images"] = [{'image': processed_image}]
67
  else:
68
- # For other spaces, pass the image as a keyword argument named 'image'
69
- kwargs["image"] = processed_image
70
-
71
- # Aspect Ratio nur bei Omni
72
- if selected_space == "Omni Image Editor" and "aspect_ratio" in cfg["inputs"]:
73
- kwargs["aspect_ratio"] = "16:9" # fest codiert wie gewΓΌnscht
74
-
75
- # API-Aufruf
76
- # args_for_predict is always empty in this implementation, so we can remove it.
77
- result = client.predict(**kwargs, api_name=cfg["api_name"])
78
-
79
- # Ergebnis verarbeiten
80
- if isinstance(result, (list, tuple)):
81
- # hΓ€ufigstes Muster: (image, status_str, ...) oder nur image
82
- for item in result:
83
- if hasattr(item, 'save'): # PIL Image
84
- return item, status + "Erfolg"
85
- if isinstance(item, str) and len(item) > 10 and "http" in item:
86
- return item, status + "Erfolg (Link)"
87
- elif hasattr(result, 'save'): # direkt PIL
88
- return result, status + "Erfolg"
89
- else:
90
- return None, status + f"Unbekanntes Ergebnis-Format: {type(result)}"
91
-
92
  except Exception as e:
93
- err_msg = str(e).replace(cfg["url"], "[URL]").replace(cfg["api_name"], "[API]")
94
- return None, status + f"Fehler: {err_msg}"
95
 
96
- # ────────────────────────────────────────────────
97
- # Gradio OberflΓ€che
98
- # ────────────────────────────────────────────────
99
  with gr.Blocks() as demo:
100
- gr.Markdown("# Multi-Space Quick Launcher\nOmni Β· FireRed Edit Β· Ana2")
101
 
102
  with gr.Row():
103
- space_choice = gr.Dropdown(
104
- choices=list(spaces.keys()),
105
- value="Omni Image Editor",
106
- label="Space auswΓ€hlen"
107
  )
 
 
 
 
 
 
 
108
 
109
- with gr.Row():
110
- with gr.Column(scale=3):
111
- prompt_input = gr.Textbox(
112
- label="Prompt (wird nur bei Omni & FireRed genutzt)",
113
- placeholder="1 oder sexy woman nude beach oder eigener Text",
114
- lines=2
115
- )
116
- with gr.Column(scale=1):
117
- generate_btn = gr.Button("AusfΓΌhren", variant="primary")
118
-
119
- image_input = gr.Image(
120
- label="Referenzbild (nur FireRed & Ana2 benΓΆtigt)",
121
- type="filepath",
122
- sources=["upload"]
123
- )
124
-
125
- output_image = gr.Image(label="Ergebnis")
126
- status_text = gr.Textbox(label="Status / Fehlermeldung", interactive=False, lines=3)
127
-
128
- # Info-Box unten
129
- gr.Markdown("""
130
- **Hinweise:**
131
- β€’ **Omni** β†’ reiner Text-to-Image (16:9 fest), braucht **kein** Bild
132
- β€’ **FireRed** β†’ Edit-Modell, braucht fast immer ein **Startbild**
133
- β€’ **Ana2** β†’ wahrscheinlich NSFW/Nudity-Analyzer, braucht nur Bild, ignoriert Prompt
134
- β€’ Bei Fehlern: API-Name oder Parameter haben sich geΓ€ndert β†’ `client.view_api()` lokal ausfΓΌhren
135
- """)
136
 
137
  generate_btn.click(
138
- fn=run_space,
139
- inputs=[space_choice, prompt_input, image_input],
140
- outputs=[output_image, status_text]
141
  )
142
 
143
  demo.launch()
 
1
  import gradio as gr
2
+ from gradio_client import Client
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
+ def generate_omni_image(prompt, aspect_ratio):
5
+ client = Client("selfit-camera/Omni-Image-Editor")
6
  try:
7
+ # Ensure aspect_ratio is a string for the API
8
+ result = client.predict(
9
+ prompt=prompt,
10
+ aspect_ratio=aspect_ratio,
11
+ api_name="/text_to_image_interface"
12
+ )
13
+ # The API returns a tuple, with the image URL usually as the first element
14
+ if isinstance(result, (list, tuple)) and result:
15
+ # The first element is often the image in HTML format or a direct URL
16
+ # Let's try to extract a direct image URL if possible from the HTML string
17
+ image_output = result[0]
18
+ if isinstance(image_output, str) and "<img src='" in image_output:
19
+ start = image_output.find("src='") + len("src='")
20
+ end = image_output.find("'", start)
21
+ image_url = image_output[start:end]
22
+ return image_url
 
 
23
  else:
24
+ return image_output # Return directly if it's a file path or URL
25
+ return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  except Exception as e:
27
+ return f"Error generating image: {e}"
 
28
 
 
 
 
29
  with gr.Blocks() as demo:
30
+ gr.Markdown("# Omni Image Editor with Gradio")
31
 
32
  with gr.Row():
33
+ prompt_input = gr.Textbox(
34
+ label="Prompt",
35
+ placeholder="Describe the image you want to generate",
36
+ lines=2
37
  )
38
+ aspect_ratio_dropdown = gr.Dropdown(
39
+ label="Aspect Ratio",
40
+ choices=["1:1", "4:3", "3:4", "16:9", "9:16", "3:2", "2:3", "5:4", "4:5", "21:9", "9:21"],
41
+ value="16:9"
42
+ )
43
+
44
+ generate_btn = gr.Button("Generate Image", variant="primary")
45
 
46
+ output_image = gr.Image(label="Generated Image", type="pil") # Changed type to 'pil' or 'filepath' for better handling
47
+ output_text = gr.Textbox(label="Status/Error", interactive=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  generate_btn.click(
50
+ fn=generate_omni_image,
51
+ inputs=[prompt_input, aspect_ratio_dropdown],
52
+ outputs=[output_image, output_text]
53
  )
54
 
55
  demo.launch()