Spaces:
Sleeping
Sleeping
File size: 2,489 Bytes
49afb29 c2651a3 a973875 c2651a3 a973875 c2651a3 a973875 49afb29 a973875 c2651a3 49afb29 a973875 49afb29 a973875 49afb29 c2651a3 49afb29 a973875 49afb29 c2651a3 49afb29 a973875 49afb29 a973875 c2651a3 a973875 49afb29 c2651a3 a973875 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 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) |