import gradio as gr from groq import Groq import os # Retrieve the API key from environment variables GROQ_API_KEY = os.environ.get('GROQ_API_KEY') # Initialize the Groq client groq_client = Groq( api_key=GROQ_API_KEY, ) # Use a valid Groq model ID groq_model_name = 'llama-3.3-70b-versatile' def generate_linkedin_post(topic, keywords, tone, call_to_action, special_request): """Generates a LinkedIn post using the Groq API based on user inputs.""" if not GROQ_API_KEY: return "Error: GROQ_API_KEY not found. Please set it as a secret in your Hugging Face Space." if not topic: return "Please provide a topic for the LinkedIn post." # Construct the prompt prompt = f"Generate a professional and engaging LinkedIn post about '{topic}'.\n" if keywords: prompt += f"Include keywords like {keywords}.\n" if tone: prompt += f"The tone should be {tone}.\n" if call_to_action: prompt += f"Conclude with a call to action: '{call_to_action}'\n" if special_request: prompt += f"Also consider this special request: '{special_request}'.\n" prompt += "\nThe post should be concise, impactful, and suitable for a professional audience." try: chat_completion = groq_client.chat.completions.create( messages=[ { "role": "user", "content": prompt, } ], model=groq_model_name, temperature=0.7, max_tokens=500 ) return chat_completion.choices[0].message.content except Exception as e: return f"An error occurred: {e}" # Create the Gradio interface iface = gr.Interface( fn=generate_linkedin_post, inputs=[ gr.Textbox(label="Topic (required)", placeholder="e.g., AI in education"), gr.Textbox(label="Keywords (optional)", placeholder="e.g., innovation, skills"), gr.Textbox(label="Tone (optional)", placeholder="e.g., professional"), gr.Textbox(label="Call to Action (optional)", placeholder="e.g., Share your thoughts!"), gr.Textbox(label="Special Request (optional)", placeholder="e.g., include #Tech") ], outputs=gr.Textbox(label="Generated LinkedIn Post", lines=10), title="🚀 LinkedIn Post Generator with Groq 🚀", description="Generate professional posts using Groq." ) if __name__ == "__main__": iface.launch(server_name="0.0.0.0", server_port=7860)