Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import io
|
|
| 5 |
import gradio as gr
|
| 6 |
from PIL import Image
|
| 7 |
|
|
|
|
| 8 |
API_KEY = os.getenv("NVIDIA_API_KEY")
|
| 9 |
INVOKE_URL = "https://ai.api.nvidia.com/v1/genai/black-forest-labs/flux.2-klein-4b"
|
| 10 |
|
|
@@ -23,21 +24,47 @@ def generate_or_edit(prompt, input_image=None):
|
|
| 23 |
"steps": 4
|
| 24 |
}
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
response = requests.post(INVOKE_URL, headers=HEADERS, json=payload)
|
| 28 |
response.raise_for_status()
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# यह लाइन बहुत महत्वपूर्ण है: यह लॉग्स में पूरा रिस्पॉन्स दिखाएगी
|
| 32 |
-
print("DEBUG RESPONSE:", data)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
# र
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
demo
|
| 43 |
-
demo.launch()
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
from PIL import Image
|
| 7 |
|
| 8 |
+
# Hugging Face Secrets से API Key लें
|
| 9 |
API_KEY = os.getenv("NVIDIA_API_KEY")
|
| 10 |
INVOKE_URL = "https://ai.api.nvidia.com/v1/genai/black-forest-labs/flux.2-klein-4b"
|
| 11 |
|
|
|
|
| 24 |
"steps": 4
|
| 25 |
}
|
| 26 |
|
| 27 |
+
# अगर इमेज है तो उसे बेस64 में बदलें
|
| 28 |
+
if input_image is not None:
|
| 29 |
+
buffered = io.BytesIO()
|
| 30 |
+
input_image.save(buffered, format="PNG")
|
| 31 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 32 |
+
payload["image"] = img_str
|
| 33 |
+
|
| 34 |
try:
|
| 35 |
response = requests.post(INVOKE_URL, headers=HEADERS, json=payload)
|
| 36 |
response.raise_for_status()
|
| 37 |
+
result = response.json()
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
# रिस्पॉन्स को प्रोसेस करना
|
| 40 |
+
# Flux मॉडल आमतौर पर 'image' या 'artifacts' में डेटा देता है
|
| 41 |
+
if "image" in result:
|
| 42 |
+
image_data = base64.b64decode(result["image"])
|
| 43 |
+
elif "artifacts" in result:
|
| 44 |
+
image_data = base64.b64decode(result["artifacts"][0]["base64"])
|
| 45 |
+
else:
|
| 46 |
+
return None
|
| 47 |
+
|
| 48 |
+
return Image.open(io.BytesIO(image_data))
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
+
print(f"Error: {e}")
|
| 52 |
+
return None
|
| 53 |
+
|
| 54 |
+
# Gradio इंटरफ़ेस
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("# Vedika AI - Image Studio")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
prompt_input = gr.Textbox(label="Prompt", placeholder="यहाँ अपना प्रॉम्प्ट लिखें...")
|
| 61 |
+
image_input = gr.Image(label="Input Image (Optional)", type="pil")
|
| 62 |
+
submit_btn = gr.Button("Generate / Edit", variant="primary")
|
| 63 |
+
|
| 64 |
+
with gr.Column():
|
| 65 |
+
result_output = gr.Image(label="Result")
|
| 66 |
+
|
| 67 |
+
submit_btn.click(fn=generate_or_edit, inputs=[prompt_input, image_input], outputs=result_output)
|
| 68 |
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|