Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Execution Plan.pdf
Binary file not shown.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# Electhon - 2023

#### Team Name -
#### Problem Statement -
#### Team Leader Email -
#### Team Name - We High Tea
#### Problem Statement - Devise a solution that can boost voter turnout on election day at the polling stations by creation of election anthems, jingles, posters etc.
#### Team Leader Email - manomayjamble15@gmail.com

## A Brief of the Prototype:
What is your solution? and how it works.
Our solution is a generative AI model that uses natural language processing (NLP) and deep learning algorithms to create unique and engaging advertisements for each community. Our solution is built on the GPT-3.5 architecture, a large language model trained by OpenAI. The model is pre-trained on a large corpus of text data, and fine-tuned on specific election-related keywords and phrases.
The input to the model is a set of parameters such as location, demographics, and popular local references. The model generates an advertisement based on these parameters, which is then reviewed by a team of human experts to ensure quality and relevance.
We have also designed a plan for efficient distrubution of generated content using google analytics and google ads.

## Code Execution Instruction:
*[If your solution is **not** application based, you can ignore this para]
`pip install -r requirements.txt`

`flask run`

*The Repository must contain your **Execution Plan PDF**.
Binary file added SakalBharati.ttf
Binary file not shown.
300 changes: 300 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@

from flask import Flask, render_template, request, redirect, url_for, send_file
import numpy as np
import requests
import json
import openai
import os
import cloudinary
import cloudinary.uploader
import textwrap
import io
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
import requests

app = Flask(__name__)


data = []

@app.route("/")
def index():
return render_template("index.html")


def push_cloudinary(data=None, url=None):
cloudinary.config(
cloud_name="dkjv1ictm",
api_key="153874947239145",
api_secret="ZkSte7mI4e1s5XNltpJw-NmMQ5Q"
)
if url!=None:
data = url
elif data!=None:
data = data
cloudinary_response = cloudinary.uploader.upload_image(
file = data,
use_filename=True,
folder="/electhon",
)

response = cloudinary_response.metadata.get("url")
if response[-3:] != "jpg":
response = response.replace(response[len(response) - 3:], "jpg")
return response


def generate_image():
url = "https://v1.genr.ai/api/circuit-element/generate-image"

payload = {
"prompt": "Citizens casting their vote in India",
"height": 512,
"width": 512,
"model": "stable-diffusion-2",
"n_images": 1
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.json())
return response.json()['output']



def get_text(prompt):

# Note: you need to be using OpenAI Python v0.27.0 for the code below to work
openai.api_key = "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"
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a creative advertisment generator."},
{"role": "user", "content": prompt},
]
)
# print(response)
return response["choices"][0]["message"]["content"]


# url = "https://v1.genr.ai/api/circuit-element/prompt-to-text"
# payload = {
# "prompt": prompt,
# }
# headers = {"Content-Type": "application/json"}
# response = requests.post(url, json=payload, headers=headers)
# print(response)
# return json.loads(response.text)["output"]


def get_text_ads(location, language, religion, age, keywords, ad_type):

if ad_type == "slogans":
slogan_prompt = f"Generate a tagline as an advertisment to attract voters. The tagline should be relatable for the people of {location} and should be in {language} language. The age distribution of is area is {age} and it is a {religion} majority. Sole local references are - {keywords}. The tagline should be limited to 2-4 lines only. Note: You don't have to use all the references in the tagline."
return {"slogans": get_text(slogan_prompt)}

if ad_type == "jingles":
jingle_prompt = f"Generate a jingle as an advertisment to attract voters. The jingle should be relatable for the people of {location} and should be in {language} language. The age distribution of is area is {age} and it is a {religion} majority. Sole local references are - {keywords}. Note: You don't have to use all the references in the jingle, the jingle should be in rhyme scheme AABB and length should not exceed 8-10 lines."
return {"jingles": get_text(jingle_prompt)}

if ad_type == "songs":
song_prompt = f"Generate a song as an advertisment to attract voters. The song should be relatable for the people of {location} and should be in {language} language. The age distribution of is area is {age} and it is a {religion} majority. Sole local references are - {keywords}. Note: You don't have to use all the references in the song. the song should have a proper tag of chorus and verse."
return {"songs": get_text(song_prompt)}

if ad_type == "posters":
poster_slogan_prompt = f"Generate a tagline as an advertisment to attract voters. The tagline should be relatable for the people of {location} and should be in {language} language. The age distribution of is area is {age} and it is a {religion} majority. Sole local references are - {keywords}. The tagline should be limited to 2-4 lines only. Note: You don't have to use all the references in the tagline."
poster_slogan = get_text(poster_slogan_prompt)
image_link = generate_image()
print(poster_slogan)

# response = requests.get(image_link)
# img = Image.open(io.BytesIO(response.content))
#
# # Create a draw object for the image
# draw = ImageDraw.Draw(img)
#
# # Set the font and font size
# font = ImageFont.truetype("arial.ttf", 20)
#
# # Get the size of the text
# text_size = draw.textsize(poster_slogan, font=font)
#
# # Calculate the position of the text
# text_x = img.width // 2 - text_size[0] // 2
# text_y = img.height - text_size[1] - 10
#
# # Draw the text on the image
# draw.text((text_x, text_y), poster_slogan, font=font, fill="white")
#
# # Save the image to a bytes buffer
# img_bytes = io.BytesIO()
# img.save(img_bytes, format='PNG')
# img_bytes.seek(0)

# # Convert the bytes buffer to a NumPy array
# img_array = np.frombuffer(img_bytes.getvalue(), dtype=np.uint8)

# image_url = push_cloudinary(img_bytes)
# return render_template("images.html", img_src=image_url)




# Download the image from the cloudinary link
response = requests.get(image_link)

# Load the image into a PIL Image object
image = Image.open(BytesIO(response.content))

# Create a draw object on the image
draw = ImageDraw.Draw(image)

# Define the text and font
text = poster_slogan
font = ImageFont.truetype("arial.ttf", 16)

# Calculate the size of the text
text_size = draw.textsize(text, font)

# Calculate the position of the text
x = (image.width - text_size[0]) // 2 # use floor division to ensure an integer value
y = image.height - text_size[1] - 10 # adjust the margin to your liking

# Draw the text on the image
draw.text((x, y), text, font=font, fill=(255, 255, 255))

img_bytes = io.BytesIO()
image.save(img_bytes, format='PNG')
img_bytes.seek(0)

image_url = push_cloudinary(img_bytes)
return render_template("images.html", img_src=image_url)



# Save the modified image
# image.save('path/to/new/image.png')







@app.route("/generate", methods=['GET', 'POST'])
def generate():
if request.method == "POST":
state = request.form.get("state")
city = request.form.get("city")
# location = request.form.get("location")
language = request.form.get("language")
religion = request.form.get("religion")
age = request.form.get("age")
keywords = request.form.get("local-references")
ad_type = request.form.get("submit-button")
print(text_on_image(get_text_ads(city, language, religion, age, keywords, ad_type), ad_type))
data.append([state, city, language, religion, age, keywords, ad_type])

print(city, language, religion,ad_type)
urls = [text_on_image(get_text_ads(city, language, religion, age, keywords, ad_type), ad_type), text_on_image(get_text_ads(city, language, religion, age, keywords, ad_type), ad_type), text_on_image(get_text_ads(city, language, religion, age, keywords, ad_type), ad_type)]
return redirect(url_for('images', img_src=urls))
else:
return render_template("generate.html")


@app.route("/regenerate", methods=['POST'])
def regenerate():
global data
temp = data[0]
state = temp[0]
city = temp[1]
language = temp[2]
religion = temp[3]
age = temp[4]
keywords = temp[5]
ad_type = temp[6]
urls = [text_on_image(get_text_ads(city, language, religion, age, keywords, ad_type), ad_type), text_on_image(get_text_ads(city, language, religion, age, keywords, ad_type), ad_type), text_on_image(get_text_ads(city, language, religion, age, keywords, ad_type), ad_type)]

return redirect(url_for('images', img_src=urls))




def text_image(height,width,text):
print(text)
image = Image.open("bg.png")

image = image.resize((height, width))


draw = ImageDraw.Draw(image)

font_size = 50
font = ImageFont.truetype("SakalBharati.ttf", font_size)

max_width = 1500

wrapped_text = textwrap.wrap(text, width=max_width/font_size)

y_text = image.height // 2 - font_size * len(wrapped_text) // 2

for line in wrapped_text:
text_width, text_height = font.getsize(line)

x_text = (image.width - text_width) // 2

draw.text((x_text, y_text), line, font=font, fill=(0, 0, 0))

y_text += text_height

img_byte_arr = io.BytesIO()
image.save(img_byte_arr, format='PNG')
img_byte_arr = img_byte_arr.getvalue()
print(push_cloudinary(img_byte_arr))
return push_cloudinary(img_byte_arr)


def text_on_image(text,addType):
if(addType == "songs"):
return text_image(1080,1920,text[addType])

elif(addType == "slogans"):
return text_image(1920,1080,text[addType])

elif(addType == "jingles"):
return text_image(1080,1920,text[addType])






@app.route("/generate.html")
def gen_page():
return render_template("generate.html")


@app.route("/index.html")
def home_page():
return render_template("index.html")



@app.route("/images")
def images():
# get the output from the query parameter
img_src = request.args.getlist('img_src')
# render the images.html template with the output
return render_template('images.html', img_src=img_src)




if __name__ == '__main__':
# print(get_text_ads("Mumbai", "English", "Hindu", "25-35", "Gateway of India, Marine drive", "song")["song"])
app.run()



Binary file added bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
aiohttp==3.8.4
aiosignal==1.3.1
async-timeout==4.0.2
attrs==22.2.0
certifi==2022.12.7
charset-normalizer==3.1.0
click==8.1.3
cloudinary==1.32.0
Flask==2.2.3
frozenlist==1.3.3
idna==3.4
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.2
multidict==6.0.4
numpy
openai==0.27.4
Pillow==9.5.0
requests==2.28.2
six==1.16.0
tqdm==4.65.0
urllib3==1.26.15
Werkzeug==2.2.3
yarl==1.8.2
gunicorn
Binary file added static/ECI.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/Election-Commission-of-India-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading