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