Deadmon commited on
Commit
ed4625b
·
verified ·
1 Parent(s): 3859164

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -25
app.py CHANGED
@@ -70,34 +70,61 @@ def generate_image(prompt, output_filename="generated_image"):
70
 
71
  return None, "No image generated"
72
 
73
- # Create Gradio interface
74
- with gr.Blocks(title="Image Generation App") as demo:
75
- gr.Markdown("# Image Generation with Gemini")
76
- gr.Markdown("Enter a prompt to generate an image using Google's Gemini model")
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  with gr.Row():
79
- with gr.Column():
80
- prompt_input = gr.Textbox(
81
- label="Prompt",
82
- placeholder="Enter your image description here...",
83
- lines=3
84
- )
85
- filename_input = gr.Textbox(
86
- label="Output Filename",
87
- value="generated_image",
88
- placeholder="Enter desired filename (without extension)"
89
- )
90
- generate_btn = gr.Button("Generate Image")
91
-
92
- with gr.Column():
93
- image_output = gr.Image(label="Generated Image")
94
- text_output = gr.Textbox(label="Status")
 
 
 
95
 
96
- # Connect the button to the generation function
97
- generate_btn.click(
98
- fn=generate_image,
99
- inputs=[prompt_input, filename_input],
100
- outputs=[image_output, text_output]
101
  )
102
 
103
  if __name__ == "__main__":
 
70
 
71
  return None, "No image generated"
72
 
73
+ # Function to handle chat interaction
74
+ def chat_handler(user_input, chat_history):
75
+ # Add user message to chat history
76
+ chat_history.append([user_input, None])
77
 
78
+ # Generate image based on user input
79
+ img, status = generate_image(user_input)
80
+
81
+ # Add AI response to chat history
82
+ if img:
83
+ chat_history.append([None, img])
84
+ chat_history.append([None, status])
85
+
86
+ return chat_history, ""
87
+
88
+ # Create Gradio interface with chatbot layout
89
+ with gr.Blocks(title="Image Editing Chatbot") as demo:
90
+ gr.Markdown("# Image Editing Chatbot")
91
+ gr.Markdown("Type a prompt to generate or edit an image using Google's Gemini model")
92
+
93
+ # Chatbot display area
94
+ chatbot = gr.Chatbot(
95
+ label="Chat",
96
+ height=400,
97
+ bubble_full_width=False,
98
+ avatar_images=(None, None) # No avatars for simplicity
99
+ )
100
+
101
+ # Input area
102
  with gr.Row():
103
+ prompt_input = gr.Textbox(
104
+ label="",
105
+ placeholder="Type something",
106
+ show_label=False,
107
+ container=False,
108
+ scale=4
109
+ )
110
+ run_btn = gr.Button("Run", scale=1)
111
+
112
+ # State to maintain chat history
113
+ chat_state = gr.State([])
114
+
115
+ # Connect the button to the chat handler
116
+ run_btn.click(
117
+ fn=chat_handler,
118
+ inputs=[prompt_input, chat_state],
119
+ outputs=[chatbot, prompt_input],
120
+ _js="() => {return [document.querySelector('input').value, null]}"
121
+ )
122
 
123
+ # Also allow Enter key to submit
124
+ prompt_input.submit(
125
+ fn=chat_handler,
126
+ inputs=[prompt_input, chat_state],
127
+ outputs=[chatbot, prompt_input]
128
  )
129
 
130
  if __name__ == "__main__":