Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
def generate_linkedin_post(topic, keywords, tone, call_to_action, special_request):
|
| 4 |
"""Generates a LinkedIn post using the Groq API based on user inputs."""
|
| 5 |
|
|
|
|
|
|
|
|
|
|
| 6 |
if not topic:
|
| 7 |
return "Please provide a topic for the LinkedIn post.\n\nExample: 'The future of AI in healthcare'"
|
| 8 |
|
| 9 |
# Construct the prompt for the Groq API
|
| 10 |
-
prompt = f"""Generate a professional and engaging LinkedIn post about '{topic}'.
|
| 11 |
-
"""
|
| 12 |
if keywords:
|
| 13 |
prompt += f"Include keywords like {keywords}.\n"
|
| 14 |
if tone:
|
|
@@ -30,19 +50,17 @@ def generate_linkedin_post(topic, keywords, tone, call_to_action, special_reques
|
|
| 30 |
}
|
| 31 |
],
|
| 32 |
model=groq_model_name,
|
| 33 |
-
temperature=0.7,
|
| 34 |
-
max_tokens=500
|
| 35 |
)
|
| 36 |
-
# Extract the generated content
|
| 37 |
return chat_completion.choices[0].message.content
|
| 38 |
except Exception as e:
|
| 39 |
-
return f"An error occurred while generating the post: {e}\n\nPlease ensure your GROQ_API_KEY is correctly set
|
| 40 |
|
| 41 |
|
| 42 |
# Create the Gradio interface
|
| 43 |
-
# We define the inputs (textboxes for topic, keywords, etc.) and the output (a textbox for the generated post)
|
| 44 |
iface = gr.Interface(
|
| 45 |
-
fn=generate_linkedin_post,
|
| 46 |
inputs=[
|
| 47 |
gr.Textbox(label="Topic (required)", placeholder="e.g., AI in education, New programming language features"),
|
| 48 |
gr.Textbox(label="Keywords (optional)", placeholder="e.g., personalized learning, future skills, innovation"),
|
|
@@ -50,13 +68,19 @@ iface = gr.Interface(
|
|
| 50 |
gr.Textbox(label="Call to Action (optional)", placeholder="e.g., Share your thoughts in the comments!, Visit our website!"),
|
| 51 |
gr.Textbox(label="Special Request (optional)", placeholder="e.g., Make it exactly 3 sentences, include a specific hashtag #FutureOfWork")
|
| 52 |
],
|
| 53 |
-
outputs=gr.Textbox(label="Generated LinkedIn Post", lines=10), #
|
| 54 |
-
title="
|
| 55 |
-
description="Generate professional and engaging LinkedIn posts using
|
| 56 |
)
|
| 57 |
|
| 58 |
# Launch the Gradio app
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
# Get the existing code for the Gradio app and correct the 'show_copy_button' error
|
| 4 |
+
gradio_app_code = """
|
| 5 |
import gradio as gr
|
| 6 |
+
from groq import Groq
|
| 7 |
+
import os # Import os for environment variables
|
| 8 |
+
|
| 9 |
+
# Retrieve the API key from environment variables (for Hugging Face Spaces)
|
| 10 |
+
# On Hugging Face, you'll add GROQ_API_KEY as a Space Secret.
|
| 11 |
+
GROQ_API_KEY = os.environ.get('GROQ_API_KEY')
|
| 12 |
+
|
| 13 |
+
# Initialize the Groq client
|
| 14 |
+
groq_client = Groq(
|
| 15 |
+
api_key=GROQ_API_KEY,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Define the Groq model to use
|
| 19 |
+
groq_model_name = 'openai/gpt-oss-120b' # Make sure this matches your chosen model
|
| 20 |
|
| 21 |
def generate_linkedin_post(topic, keywords, tone, call_to_action, special_request):
|
| 22 |
"""Generates a LinkedIn post using the Groq API based on user inputs."""
|
| 23 |
|
| 24 |
+
if not GROQ_API_KEY:
|
| 25 |
+
return "Error: GROQ_API_KEY not found. Please set it as a secret in your Hugging Face Space."
|
| 26 |
+
|
| 27 |
if not topic:
|
| 28 |
return "Please provide a topic for the LinkedIn post.\n\nExample: 'The future of AI in healthcare'"
|
| 29 |
|
| 30 |
# Construct the prompt for the Groq API
|
| 31 |
+
prompt = f"""Generate a professional and engaging LinkedIn post about '{topic}'.\n"""
|
|
|
|
| 32 |
if keywords:
|
| 33 |
prompt += f"Include keywords like {keywords}.\n"
|
| 34 |
if tone:
|
|
|
|
| 50 |
}
|
| 51 |
],
|
| 52 |
model=groq_model_name,
|
| 53 |
+
temperature=0.7,
|
| 54 |
+
max_tokens=500
|
| 55 |
)
|
|
|
|
| 56 |
return chat_completion.choices[0].message.content
|
| 57 |
except Exception as e:
|
| 58 |
+
return f"An error occurred while generating the post: {e}\n\nPlease ensure your GROQ_API_KEY is correctly set as a secret and you have internet access."
|
| 59 |
|
| 60 |
|
| 61 |
# Create the Gradio interface
|
|
|
|
| 62 |
iface = gr.Interface(
|
| 63 |
+
fn=generate_linkedin_post,
|
| 64 |
inputs=[
|
| 65 |
gr.Textbox(label="Topic (required)", placeholder="e.g., AI in education, New programming language features"),
|
| 66 |
gr.Textbox(label="Keywords (optional)", placeholder="e.g., personalized learning, future skills, innovation"),
|
|
|
|
| 68 |
gr.Textbox(label="Call to Action (optional)", placeholder="e.g., Share your thoughts in the comments!, Visit our website!"),
|
| 69 |
gr.Textbox(label="Special Request (optional)", placeholder="e.g., Make it exactly 3 sentences, include a specific hashtag #FutureOfWork")
|
| 70 |
],
|
| 71 |
+
outputs=gr.Textbox(label="Generated LinkedIn Post", lines=10), # Removed show_copy_button=True
|
| 72 |
+
title="🚀 LinkedIn Post Generator with Groq 🚀",
|
| 73 |
+
description="Generate professional and engaging LinkedIn posts using the powerful Groq API. Fill in the details below and click 'Submit' to get your post!"
|
| 74 |
)
|
| 75 |
|
| 76 |
# Launch the Gradio app
|
| 77 |
+
# For Hugging Face Spaces, Gradio will automatically handle the launch configuration.
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
| 80 |
+
"""
|
| 81 |
+
|
| 82 |
+
# Write the content to app.py
|
| 83 |
+
with open('app.py', 'w') as f:
|
| 84 |
+
f.write(gradio_app_code)
|
| 85 |
+
|
| 86 |
+
print("Created app.py with corrected code.")
|