Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,10 +6,15 @@ from indic_transliteration.detect import detect as detect_script
|
|
6 |
from indic_transliteration.sanscript import transliterate
|
7 |
import langdetect
|
8 |
import re
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Initialize clients
|
11 |
text_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
12 |
-
|
13 |
|
14 |
def detect_language_script(text: str) -> tuple[str, str]:
|
15 |
"""Detect language and script of the input text.
|
@@ -111,20 +116,56 @@ def is_image_request(message: str) -> bool:
|
|
111 |
message_lower = message.lower()
|
112 |
return any(trigger in message_lower for trigger in image_triggers)
|
113 |
|
114 |
-
def
|
115 |
-
"""Generate an image using DALLE-4K
|
116 |
try:
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
except Exception as e:
|
129 |
print(f"Image generation error: {e}")
|
130 |
return None
|
@@ -174,11 +215,9 @@ def respond(
|
|
174 |
# Check if this is an image generation request
|
175 |
if is_image_request(message):
|
176 |
try:
|
177 |
-
image =
|
178 |
if image:
|
179 |
-
yield f"Here's your generated image based on: {message}"
|
180 |
-
# You'll need to implement the actual image display logic
|
181 |
-
# depending on your Gradio interface requirements
|
182 |
return
|
183 |
else:
|
184 |
yield "Sorry, I couldn't generate the image. Please try again."
|
@@ -258,7 +297,4 @@ demo = gr.ChatInterface(
|
|
258 |
label="Top-p (nucleus sampling)"
|
259 |
),
|
260 |
]
|
261 |
-
)
|
262 |
-
|
263 |
-
if __name__ == "__main__":
|
264 |
-
demo.launch(share=True)
|
|
|
6 |
from indic_transliteration.sanscript import transliterate
|
7 |
import langdetect
|
8 |
import re
|
9 |
+
import requests
|
10 |
+
import json
|
11 |
+
import base64
|
12 |
+
from PIL import Image
|
13 |
+
import io
|
14 |
|
15 |
# Initialize clients
|
16 |
text_client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
17 |
+
SPACE_URL = "https://ijohn07-dalle-4k.hf.space"
|
18 |
|
19 |
def detect_language_script(text: str) -> tuple[str, str]:
|
20 |
"""Detect language and script of the input text.
|
|
|
116 |
message_lower = message.lower()
|
117 |
return any(trigger in message_lower for trigger in image_triggers)
|
118 |
|
119 |
+
def generate_image_space(prompt: str) -> Image.Image:
|
120 |
+
"""Generate an image using the DALLE-4K Space."""
|
121 |
try:
|
122 |
+
# First get the session hash
|
123 |
+
response = requests.post(f"{SPACE_URL}/queue/join")
|
124 |
+
session_hash = response.json().get('session_hash')
|
125 |
+
|
126 |
+
# Send the generation request
|
127 |
+
payload = {
|
128 |
+
"prompt": prompt,
|
129 |
+
"negative_prompt": "blurry, bad quality, nsfw",
|
130 |
+
"num_inference_steps": 30,
|
131 |
+
"guidance_scale": 7.5,
|
132 |
+
"session_hash": session_hash
|
133 |
+
}
|
134 |
+
|
135 |
+
response = requests.post(f"{SPACE_URL}/run/predict", json={
|
136 |
+
"data": [
|
137 |
+
prompt, # Prompt
|
138 |
+
"", # Negative prompt
|
139 |
+
7.5, # Guidance scale
|
140 |
+
30, # Steps
|
141 |
+
"DPM++ SDE Karras", # Scheduler
|
142 |
+
False, # High resolution
|
143 |
+
False, # Image to image
|
144 |
+
None, # Image upload
|
145 |
+
1 # Batch size
|
146 |
+
],
|
147 |
+
"session_hash": session_hash
|
148 |
+
})
|
149 |
+
|
150 |
+
# Poll for results
|
151 |
+
while True:
|
152 |
+
status_response = requests.post(f"{SPACE_URL}/queue/status", json={
|
153 |
+
"session_hash": session_hash
|
154 |
+
})
|
155 |
+
status_data = status_response.json()
|
156 |
+
|
157 |
+
if status_data.get('status') == 'complete':
|
158 |
+
# Get the image data
|
159 |
+
image_data = status_data['data']['image']
|
160 |
+
# Convert base64 to PIL Image
|
161 |
+
image_bytes = base64.b64decode(image_data.split(',')[1])
|
162 |
+
image = Image.open(io.BytesIO(image_bytes))
|
163 |
+
return image
|
164 |
+
elif status_data.get('status') == 'error':
|
165 |
+
raise Exception(f"Image generation failed: {status_data.get('error')}")
|
166 |
+
|
167 |
+
time.sleep(1) # Wait before polling again
|
168 |
+
|
169 |
except Exception as e:
|
170 |
print(f"Image generation error: {e}")
|
171 |
return None
|
|
|
215 |
# Check if this is an image generation request
|
216 |
if is_image_request(message):
|
217 |
try:
|
218 |
+
image = generate_image_space(message)
|
219 |
if image:
|
220 |
+
yield (image, f"Here's your generated image based on: {message}")
|
|
|
|
|
221 |
return
|
222 |
else:
|
223 |
yield "Sorry, I couldn't generate the image. Please try again."
|
|
|
297 |
label="Top-p (nucleus sampling)"
|
298 |
),
|
299 |
]
|
300 |
+
)
|
|
|
|
|
|