import os import gradio as gr from groq import Groq # Fetch the key from Hugging Face Secrets GROQ_API_KEY = os.environ.get("GROQ_API_KEY") client = Groq(api_key=GROQ_API_KEY) def generate_linkedin_post(topic, tone): # Using the updated Llama 3.1 model model_name = "llama-3.1-8b-instant" prompt = f"Write a professional LinkedIn post about '{topic}'. The tone should be {tone}. Include relevant emojis and 3-5 hashtags." try: completion = client.chat.completions.create( model=model_name, messages=[ {"role": "system", "content": "You are an expert content creator for LinkedIn."}, {"role": "user", "content": prompt} ], temperature=0.7, max_tokens=1024, ) return completion.choices[0].message.content except Exception as e: return f"Error: {str(e)}" # UI Design with gr.Blocks() as demo: gr.Markdown("# 🚀 AI LinkedIn Post Generator") with gr.Row(): topic_input = gr.Textbox(label="What is the post about?") tone_input = gr.Dropdown( choices=["Professional", "Inspirational", "Witty", "Educational"], value="Professional", label="Tone" ) generate_btn = gr.Button("Generate Post", variant="primary") output_text = gr.Textbox(label="Generated Post", lines=10) generate_btn.click(fn=generate_linkedin_post, inputs=[topic_input, tone_input], outputs=output_text) # Launch (Hugging Face handles the port automatically) demo.launch()