Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,105 @@
|
|
| 1 |
-
import google.generativeai as genai
|
| 2 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
response = model.generate_content(
|
| 8 |
-
text_input="the color of the car is ?",
|
| 9 |
-
image_input="car.jpg"
|
| 10 |
-
)
|
| 11 |
-
print(response)
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# Load environment variables from .env file
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# Get the API key from the environment
|
| 11 |
+
API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 12 |
+
|
| 13 |
+
# Set up the model with the API key
|
| 14 |
+
genai.configure(api_key=API_KEY)
|
| 15 |
+
|
| 16 |
+
# Set up the model
|
| 17 |
+
generation_config = {
|
| 18 |
+
"temperature": 0.7,
|
| 19 |
+
"top_p": 0.9,
|
| 20 |
+
"top_k": 40,
|
| 21 |
+
"max_output_tokens": 4000,
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
safety_settings = [
|
| 25 |
+
{
|
| 26 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
| 27 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 28 |
+
},
|
| 29 |
+
{
|
| 30 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
| 31 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
| 35 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 36 |
+
},
|
| 37 |
+
{
|
| 38 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
| 39 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
| 40 |
+
}
|
| 41 |
+
]
|
| 42 |
+
|
| 43 |
+
model = genai.GenerativeModel(model_name="gemini-pro-vision",
|
| 44 |
+
generation_config=generation_config,
|
| 45 |
+
safety_settings=safety_settings)
|
| 46 |
+
|
| 47 |
+
def input_image_setup(file_loc):
|
| 48 |
+
if not (img := Path(file_loc)).exists():
|
| 49 |
+
raise FileNotFoundError(f"Could not find image: {img}")
|
| 50 |
+
|
| 51 |
+
image_parts = [
|
| 52 |
+
{
|
| 53 |
+
"mime_type": "image/jpeg",
|
| 54 |
+
"data": Path(file_loc).read_bytes()
|
| 55 |
+
}
|
| 56 |
+
]
|
| 57 |
+
return image_parts
|
| 58 |
+
|
| 59 |
+
def generate_gemini_response(input_prompt, text_input, image_loc):
|
| 60 |
+
image_prompt = input_image_setup(image_loc)
|
| 61 |
+
prompt_parts = [input_prompt + text_input, image_prompt[0]]
|
| 62 |
+
response = model.generate_content(prompt_parts)
|
| 63 |
+
return response.text
|
| 64 |
+
|
| 65 |
+
input_prompt = """You are an advanced AI model specializing in generating engaging and contextually relevant captions or post content for social media platforms.
|
| 66 |
+
Given the context and an uploaded image, your task is to create a captivating caption or post content that resonates with the selected social media platform's audience.
|
| 67 |
+
Please analyze the provided image and the contextual description carefully. Use the following guidelines based on the social media platform specified:
|
| 68 |
+
|
| 69 |
+
1. **Instagram**: Focus on visually appealing, inspirational, and trendy content. Use relevant hashtags.
|
| 70 |
+
2. **Facebook**: Craft engaging and personal stories or updates. Aim for a friendly and conversational tone.
|
| 71 |
+
3. **Twitter**: Create concise, witty, and impactful tweets. Utilize popular hashtags and mentions.
|
| 72 |
+
4. **LinkedIn**: Develop professional and insightful posts. Emphasize expertise, industry relevance, and networking.
|
| 73 |
+
5. **Pinterest**: Write creative and informative descriptions. Highlight the aesthetic and practical aspects.
|
| 74 |
+
|
| 75 |
+
Output should be one after another caption/post (bulleted in case of more than 1)
|
| 76 |
+
|
| 77 |
+
The prompted message is: """
|
| 78 |
+
|
| 79 |
+
def upload_file(files, text_input, social_media_platform, num_captions=1):
|
| 80 |
+
if not files:
|
| 81 |
+
return None, "Image not uploaded"
|
| 82 |
+
file_paths = [file.name for file in files]
|
| 83 |
+
response = generate_gemini_response(input_prompt + f"\nPlatform: {social_media_platform}\n"+ f"Number of Captions/Posts: {num_captions}\n", text_input, file_paths[0])
|
| 84 |
+
return file_paths[0], response
|
| 85 |
|
| 86 |
+
with gr.Blocks() as demo:
|
| 87 |
+
header = gr.Label("Captionify: From Image to Engagement - Get the Perfect Caption!")
|
| 88 |
+
text_input = gr.Textbox(label="Enter context for the image")
|
| 89 |
+
social_media_input = gr.Dropdown(choices=["Instagram", "Facebook", "Twitter", "LinkedIn", "Pinterest"], label="Select social media platform")
|
| 90 |
+
num_captions_input = gr.Number(label="Number of Captions/Posts")
|
| 91 |
+
image_output = gr.Image()
|
| 92 |
+
upload_button = gr.UploadButton("Click to upload an image", file_types=["image"], file_count="multiple")
|
| 93 |
+
generate_button = gr.Button("Generate")
|
| 94 |
+
|
| 95 |
+
file_output = gr.Textbox(label="Generated Caption/Post Content")
|
| 96 |
+
|
| 97 |
+
def process_generate(files, text_input, social_media_input, num_captions_input):
|
| 98 |
+
if not files:
|
| 99 |
+
return None, "Image not uploaded"
|
| 100 |
+
return upload_file(files, text_input, social_media_input, num_captions_input)
|
| 101 |
+
|
| 102 |
+
upload_button.upload(fn=lambda files: files[0].name if files else None, inputs=[upload_button], outputs=image_output)
|
| 103 |
+
generate_button.click(fn=process_generate, inputs=[upload_button, text_input, social_media_input, num_captions_input], outputs=[image_output, file_output])
|
| 104 |
|
| 105 |
+
demo.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|