amoghsuman's picture
Update app.py
fa4478f verified
import openai
from openai import OpenAI
import gradio as gr
import os
from dotenv import load_dotenv
# Load your .env variables
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def generate_content(topic, keywords, tone="Professional"):
"""
Uses OpenAI to generate AI-powered content based on a topic.
"""
prompt = f"Write a blog post about '{topic}' in a {tone} tone.\n\n" \
f"Include the following keywords: {keywords}.\n\n" \
f"Ensure the content is well-structured with an introduction, main sections, and a conclusion."
try:
response = client.chat.completions.create(
model="gpt-4", # or "gpt-3.5-turbo"
messages=[
{"role": "system", "content": "You are a professional blog writer."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=800
)
return response.choices[0].message.content
except Exception as e:
return f"Error: {e}"
def generate_social_media_post(topic, platform="Twitter"):
prompt = f"Write an engaging social media post for {platform} about '{topic}'. Keep it short and catchy."
try:
response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a creative social media strategist."},
{"role": "user", "content": prompt}
],
temperature=0.9,
max_tokens=150
)
return response.choices[0].message.content
except Exception as e:
return f"Error: {e}"
# ✅ Gradio Interface
interface = gr.Interface(
fn=generate_content,
inputs=[
gr.Textbox(label="Topic"),
gr.Textbox(label="Keywords (comma-separated)"),
gr.Radio(["Professional", "Casual", "Persuasive"], label="Tone"),
],
outputs=gr.Textbox(label="Generated Content"),
title="AI-Powered Content Writer",
description="Enter a topic, keywords, and tone, and AI will generate a blog post or marketing content."
)
# ✅ Launch
if __name__ == "__main__":
interface.launch()