Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,8 @@
|
|
| 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
|
| 8 |
|
| 9 |
-
# Retrieve the API key from environment variables
|
| 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
|
|
@@ -15,20 +10,20 @@ groq_client = Groq(
|
|
| 15 |
api_key=GROQ_API_KEY,
|
| 16 |
)
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
groq_model_name = '
|
| 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.
|
| 29 |
|
| 30 |
-
# Construct the prompt
|
| 31 |
-
prompt = f"
|
| 32 |
if keywords:
|
| 33 |
prompt += f"Include keywords like {keywords}.\n"
|
| 34 |
if tone:
|
|
@@ -41,7 +36,6 @@ def generate_linkedin_post(topic, keywords, tone, call_to_action, special_reques
|
|
| 41 |
prompt += "\nThe post should be concise, impactful, and suitable for a professional audience."
|
| 42 |
|
| 43 |
try:
|
| 44 |
-
# Make the API call to Groq
|
| 45 |
chat_completion = groq_client.chat.completions.create(
|
| 46 |
messages=[
|
| 47 |
{
|
|
@@ -55,32 +49,22 @@ def generate_linkedin_post(topic, keywords, tone, call_to_action, special_reques
|
|
| 55 |
)
|
| 56 |
return chat_completion.choices[0].message.content
|
| 57 |
except Exception as e:
|
| 58 |
-
return f"An error occurred
|
| 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
|
| 66 |
-
gr.Textbox(label="Keywords (optional)", placeholder="e.g.,
|
| 67 |
-
gr.Textbox(label="Tone (optional)", placeholder="e.g., professional
|
| 68 |
-
gr.Textbox(label="Call to Action (optional)", placeholder="e.g., Share your thoughts
|
| 69 |
-
gr.Textbox(label="Special Request (optional)", placeholder="e.g.,
|
| 70 |
],
|
| 71 |
-
outputs=gr.Textbox(label="Generated LinkedIn Post", lines=10),
|
| 72 |
title="π LinkedIn Post Generator with Groq π",
|
| 73 |
-
description="Generate professional
|
| 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.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
| 3 |
+
import os
|
| 4 |
|
| 5 |
+
# Retrieve the API key from environment variables
|
|
|
|
| 6 |
GROQ_API_KEY = os.environ.get('GROQ_API_KEY')
|
| 7 |
|
| 8 |
# Initialize the Groq client
|
|
|
|
| 10 |
api_key=GROQ_API_KEY,
|
| 11 |
)
|
| 12 |
|
| 13 |
+
# Use a valid Groq model ID
|
| 14 |
+
groq_model_name = 'llama-3.3-70b-versatile'
|
| 15 |
|
| 16 |
def generate_linkedin_post(topic, keywords, tone, call_to_action, special_request):
|
| 17 |
"""Generates a LinkedIn post using the Groq API based on user inputs."""
|
| 18 |
+
|
| 19 |
if not GROQ_API_KEY:
|
| 20 |
return "Error: GROQ_API_KEY not found. Please set it as a secret in your Hugging Face Space."
|
| 21 |
|
| 22 |
if not topic:
|
| 23 |
+
return "Please provide a topic for the LinkedIn post."
|
| 24 |
|
| 25 |
+
# Construct the prompt
|
| 26 |
+
prompt = f"Generate a professional and engaging LinkedIn post about '{topic}'.\n"
|
| 27 |
if keywords:
|
| 28 |
prompt += f"Include keywords like {keywords}.\n"
|
| 29 |
if tone:
|
|
|
|
| 36 |
prompt += "\nThe post should be concise, impactful, and suitable for a professional audience."
|
| 37 |
|
| 38 |
try:
|
|
|
|
| 39 |
chat_completion = groq_client.chat.completions.create(
|
| 40 |
messages=[
|
| 41 |
{
|
|
|
|
| 49 |
)
|
| 50 |
return chat_completion.choices[0].message.content
|
| 51 |
except Exception as e:
|
| 52 |
+
return f"An error occurred: {e}"
|
|
|
|
| 53 |
|
| 54 |
# Create the Gradio interface
|
| 55 |
iface = gr.Interface(
|
| 56 |
fn=generate_linkedin_post,
|
| 57 |
inputs=[
|
| 58 |
+
gr.Textbox(label="Topic (required)", placeholder="e.g., AI in education"),
|
| 59 |
+
gr.Textbox(label="Keywords (optional)", placeholder="e.g., innovation, skills"),
|
| 60 |
+
gr.Textbox(label="Tone (optional)", placeholder="e.g., professional"),
|
| 61 |
+
gr.Textbox(label="Call to Action (optional)", placeholder="e.g., Share your thoughts!"),
|
| 62 |
+
gr.Textbox(label="Special Request (optional)", placeholder="e.g., include #Tech")
|
| 63 |
],
|
| 64 |
+
outputs=gr.Textbox(label="Generated LinkedIn Post", lines=10),
|
| 65 |
title="π LinkedIn Post Generator with Groq π",
|
| 66 |
+
description="Generate professional posts using Groq."
|
| 67 |
)
|
| 68 |
|
|
|
|
|
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|