Vedika-AI commited on
Commit
277d8e8
·
verified ·
1 Parent(s): 2e8b7bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -11
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
- data = response.json()
30
-
31
- # यह लाइन बहुत महत्वपूर्ण है: यह लॉग्स में पूरा रिस्पॉन्स दिखाएगी
32
- print("DEBUG RESPONSE:", data)
33
 
34
- # अग इमेज 'image' की में नहीं है, तो रिस्पॉन्स क ट्क्चर कुसर इसे बदलें
35
- # रिस्ॉन्स लॉग में खें कि इमज कह है (जैसे: data['output']['image'])
36
- return "Check Logs for: " + str(list(data.keys()))
 
 
 
 
 
 
 
37
 
38
  except Exception as e:
39
- return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- # इंटरफ़ेस
42
- demo = gr.Interface(fn=generate_or_edit, inputs=["text", "image"], outputs="text")
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)