Earnings / app.py
Lucky164's picture
Update app.py
654f274 verified
import requests
import gradio as gr
import os
from dotenv import load_dotenv
load_dotenv()
# API Credentials
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
# API URLs
OPENROUTER_API_URL = "https://openrouter.ai/api/v1/chat/completions"
HUGGINGFACE_MODEL_ID = "black-forest-labs/FLUX.1-dev"
HUGGINGFACE_API_URL = f"https://api-inference.huggingface.co/models/{HUGGINGFACE_MODEL_ID}"
# Headers
openrouter_headers = {
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"Content-Type": "application/json"
}
huggingface_headers = {
"Authorization": f"Bearer {HUGGINGFACE_API_KEY}"
}
# πŸ”Ή Function: Generate Business Names & Slogans
def generate_business_name(business_type):
prompt = (
f"Generate 5 unique, creative, and brandable business names with slogans for a {business_type}. "
"Each name should be catchy, professional, and memorable. Format the output like:\n"
"1. BusinessName - \"Slogan\"\n"
"2. BusinessName - \"Slogan\"\n"
"...\n"
"Then, recommend the best name from these."
)
payload = {
"model": "mistralai/mistral-7b-instruct",
"messages": [{"role": "user", "content": prompt}]
}
response = requests.post(OPENROUTER_API_URL, headers=openrouter_headers, json=payload)
if response.status_code == 200:
result = response.json()
generated_text = result["choices"][0]["message"]["content"].strip()
# Extract business names
business_names = []
lines = generated_text.split("\n")
for line in lines:
if line.startswith(("1.", "2.", "3.", "4.", "5.")):
business_names.append(line.strip())
# Extract the best suggested name
best_name = lines[-1].split(":")[-1].strip() if ":" in lines[-1] else business_names[0]
return "\n".join(business_names), best_name
else:
return f"Error {response.status_code}: {response.json().get('error', 'Failed to generate names')}", None
# πŸ”Ή Function: Generate Logo Image
def generate_logo(business_name):
prompt = (
f"Create a high-definition 8K logo for '{business_name}'. "
"Make it modern, minimalistic, and sleek with a technology-inspired color palette. "
"Use elements like glowing lines and a clean, elegant font."
)
data = {"inputs": prompt}
response = requests.post(HUGGINGFACE_API_URL, headers=huggingface_headers, json=data)
if response.status_code == 200:
with open("generated_logo.png", "wb") as f:
f.write(response.content)
return "generated_logo.png"
else:
return None
# πŸ”Ή Gradio Interface
def gradio_interface(business_type):
business_names, best_name = generate_business_name(business_type)
if business_names.startswith("Error"):
return business_names, None
logo_path = generate_logo(best_name) if best_name else None
return business_names, logo_path
# πŸ”Ή Gradio UI with Improved Design
with gr.Blocks(css="""
body { font-family: 'Poppins', sans-serif; background-color: #f5f5f5; }
h1 { text-align: center; color: #007bff; font-weight: 700; font-size: 2.5em; }
h3 { text-align: center; font-weight: 500; font-size: 1.3em; color: #333; }
.gradio-container { max-width: 900px; margin: auto; background: white; padding: 25px; border-radius: 10px; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1); }
.gr-button { background: #007bff; color: white; font-weight: bold; border-radius: 8px; }
.gr-button:hover { background: #0056b3; }
""") as demo:
gr.Markdown("<h1>πŸš€ Business Name & Logo Generator</h1>"
"<h3>Generate 5 Unique Business Names, Slogans & a Logo Instantly!</h3>")
with gr.Row():
business_input = gr.Textbox(label="πŸ”Ή Enter Business Type", placeholder="e.g., Tech Startup, AI Company",
lines=1, interactive=True, show_label=False)
with gr.Row():
generate_button = gr.Button("🎨 Generate", variant="primary")
# Row for Business Names & Logo Side-by-Side
with gr.Row():
output_text = gr.Textbox(label="πŸ”Ή Generated Business Names & Slogans", interactive=False, lines=10)
output_logo = gr.Image(label="🎨 Generated Logo", type="filepath", interactive=False, height=400, width=400)
generate_button.click(gradio_interface, inputs=business_input, outputs=[output_text, output_logo])
# Run the app
demo.launch()