Arjun Singh commited on
Commit
bd6ad77
·
1 Parent(s): 135be1c

New Image Generation app

Browse files
Files changed (2) hide show
  1. app.py +63 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import base64
4
+ from PIL import Image
5
+ import io
6
+ import json
7
+ import os
8
+ #from dotenv import load_dotenv
9
+
10
+ #dotenv_path = '/.env'
11
+ #load_dotenv(dotenv_path)
12
+
13
+ # Get the API key from the environment variable
14
+ API_KEY = os.getenv('BASETEN_API_KEY')
15
+ API_url = os.getenv('BASETEN_COMFY_MODEL_URL')
16
+
17
+ def call_api_and_generate_image(negative_prompt, positive_prompt):
18
+ if not API_KEY:
19
+ return None, "Error: API key not found in environment variables. Please set BASETEN_API_KEY."
20
+
21
+ resp = requests.post(
22
+ API_url,
23
+ headers={"Authorization": f"Api-Key {API_KEY}"},
24
+ json={
25
+ 'workflow_values': {
26
+ 'negative_prompt': negative_prompt,
27
+ 'positive_prompt': positive_prompt
28
+ }
29
+ }
30
+ )
31
+
32
+ result = resp.json()
33
+ base64_data = result['result'][0]['data']
34
+ image_data = base64.b64decode(base64_data)
35
+ image = Image.open(io.BytesIO(image_data))
36
+ return image, json.dumps(result, indent=2)
37
+
38
+ def generate_image(positive_prompt, negative_prompt):
39
+ try:
40
+ return call_api_and_generate_image(negative_prompt, positive_prompt)
41
+ except Exception as e:
42
+ return None, f"Error: {str(e)}"
43
+
44
+ def clear_fields():
45
+ return "", "", "" # Clear prompt and output, but not the API key
46
+
47
+ with gr.Blocks(theme='freddyaboulton/test-blue') as demo:
48
+ gr.Markdown("<center><h2>Arjun's Image Generator</h2></center>")
49
+ gr.Markdown("Hi there! I'm an AI assistant tasked with generating images.")
50
+ prompt = gr.Textbox(label='Positve Prompt', lines=2, max_lines=5, placeholder = 'Describe your image here.')
51
+ neg_prompt = gr.Textbox(label='Negative Prompt', lines=2, max_lines=10, value="Low quality, pixelated")
52
+
53
+ with gr.Group():
54
+ with gr.Row():
55
+ submit_btn = gr.Button(value="Submit", elem_id="generate_button", variant="primary", size="sm")
56
+ clear_btn = gr.ClearButton(value="Clear Question and AI Response", elem_id="clear_button", variant="secondary", size="sm")
57
+ gr.Markdown("<center><h3>AI Response</h3></center>")
58
+ image_output = gr.Image(type="pil", label="Generated Image")
59
+ json_output = gr.Textbox(label="API Response JSON", lines=10)
60
+ submit_btn.click(fn=generate_image, inputs = [prompt,neg_prompt], outputs=[image_output,json_output])
61
+ clear_btn.click(fn=clear_fields,outputs=[prompt,image_output,json_output, neg_prompt])
62
+
63
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ requests
3
+ base64
4
+ PIL