Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,82 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Initialize Groq client
|
| 5 |
-
client = Groq()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def generate_ai_updates():
|
|
|
|
| 8 |
try:
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
messages=[
|
| 11 |
{
|
| 12 |
"role": "system",
|
| 13 |
-
"content":
|
| 14 |
},
|
| 15 |
{
|
| 16 |
"role": "user",
|
| 17 |
-
"content": "Please provide the latest AI developments and trends."
|
| 18 |
}
|
| 19 |
],
|
| 20 |
-
model="llama3-70b-8192"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
)
|
| 22 |
-
return
|
| 23 |
except Exception as e:
|
| 24 |
return f"Error: {str(e)}"
|
| 25 |
|
| 26 |
-
# Create
|
| 27 |
-
with gr.Blocks() as demo:
|
| 28 |
gr.Markdown("# AI Content Curator")
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
import base64
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
# Load environment variables
|
| 9 |
+
load_dotenv()
|
| 10 |
|
| 11 |
# Initialize Groq client
|
| 12 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 13 |
+
|
| 14 |
+
def load_system_prompt():
|
| 15 |
+
"""Load and decode the system prompt from the config file"""
|
| 16 |
+
try:
|
| 17 |
+
# Get the directory of the current script
|
| 18 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 19 |
+
config_path = os.path.join(current_dir, "config", "system_prompt.txt")
|
| 20 |
+
|
| 21 |
+
# Read the system prompt
|
| 22 |
+
with open(config_path, 'r') as file:
|
| 23 |
+
return file.read().strip()
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"Error loading system prompt: {str(e)}")
|
| 26 |
+
return "You are a helpful social media research assistant."
|
| 27 |
+
|
| 28 |
+
# Load the system prompt once when the app starts
|
| 29 |
+
SYSTEM_PROMPT = load_system_prompt()
|
| 30 |
|
| 31 |
def generate_ai_updates():
|
| 32 |
+
"""Generate AI updates using Groq API"""
|
| 33 |
try:
|
| 34 |
+
# Get current date for context
|
| 35 |
+
current_date = datetime.now().strftime("%B %d, %Y")
|
| 36 |
+
|
| 37 |
+
chat_completion = client.chat.completions.create(
|
| 38 |
messages=[
|
| 39 |
{
|
| 40 |
"role": "system",
|
| 41 |
+
"content": SYSTEM_PROMPT
|
| 42 |
},
|
| 43 |
{
|
| 44 |
"role": "user",
|
| 45 |
+
"content": f"Please analyze and provide the latest AI developments and trends for {current_date}. Follow the verification workflow and create content as specified in the system prompt."
|
| 46 |
}
|
| 47 |
],
|
| 48 |
+
model="llama3-70b-8192",
|
| 49 |
+
temperature=0.7,
|
| 50 |
+
max_tokens=2048,
|
| 51 |
+
top_p=0.9,
|
| 52 |
+
frequency_penalty=0.1,
|
| 53 |
+
presence_penalty=0.1
|
| 54 |
)
|
| 55 |
+
return chat_completion.choices[0].message.content
|
| 56 |
except Exception as e:
|
| 57 |
return f"Error: {str(e)}"
|
| 58 |
|
| 59 |
+
# Create Gradio interface
|
| 60 |
+
with gr.Blocks(title="AI Content Curator") as demo:
|
| 61 |
gr.Markdown("# AI Content Curator")
|
| 62 |
+
gr.Markdown("Click the button below to get the latest AI developments and content recommendations.")
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
with gr.Column():
|
| 66 |
+
submit_btn = gr.Button("Generate Latest AI Updates")
|
| 67 |
+
|
| 68 |
+
with gr.Column():
|
| 69 |
+
output = gr.Textbox(
|
| 70 |
+
label="AI Updates and Content Recommendations",
|
| 71 |
+
lines=20,
|
| 72 |
+
interactive=False
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
submit_btn.click(
|
| 76 |
+
fn=generate_ai_updates,
|
| 77 |
+
inputs=[],
|
| 78 |
+
outputs=output
|
| 79 |
+
)
|
| 80 |
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
demo.launch(share=True)
|