James040 commited on
Commit
2a9394d
·
verified ·
1 Parent(s): adfca9f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ from PIL import Image
4
+
5
+ # 1. Initialize the Image-to-Text pipeline
6
+ # This will download the model (approx 900MB) on the first run
7
+ get_prompt = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
8
+
9
+ def generate_prompt(input_img):
10
+ if input_img is None:
11
+ return "Please upload an image."
12
+ try:
13
+ # 2. Process the image
14
+ result = get_prompt(input_img)
15
+ # The model returns a list: [{'generated_text': 'a prompt description'}]
16
+ return result[0]['generated_text']
17
+ except Exception as e:
18
+ return f"Error: {str(e)}"
19
+
20
+ # 3. Build the Interface
21
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
22
+ gr.Markdown("# 🔍 Image to Prompt Interrogator")
23
+ with gr.Row():
24
+ img_input = gr.Image(type="pil", label="Upload Image")
25
+ text_output = gr.Textbox(label="Generated AI Prompt", interactive=False, show_copy_button=True)
26
+
27
+ btn = gr.Button("Analyze Image", variant="primary")
28
+
29
+ # Declare API name for the blog
30
+ btn.click(fn=generate_prompt, inputs=img_input, outputs=text_output, api_name="get_prompt")
31
+
32
+ # 4. Launch with stable network settings
33
+ if __name__ == "__main__":
34
+ demo.queue(default_concurrency_limit=1).launch(server_name="0.0.0.0", server_port=7860, ssr_mode=False)