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