setiyanikhil3's picture
Update app.py
684e707 verified
raw
history blame
2.5 kB
import os
import gradio as gr
import groq
from dotenv import load_dotenv
from datetime import datetime
# Load environment variables
load_dotenv()
# Set API Key for Groq globally
groq.api_key = os.getenv("GROQ_API_KEY")
def load_system_prompt():
try:
current_dir = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(current_dir, "config", "system_prompt.txt")
with open(config_path, 'r') as file:
return file.read().strip()
except Exception as e:
print(f"Error loading system prompt: {str(e)}")
return "You are a helpful social media research assistant."
SYSTEM_PROMPT = load_system_prompt()
def generate_ai_updates():
"""Generate AI updates using Groq API"""
try:
current_date = datetime.now().strftime("%B %d, %Y")
response = groq.chat(
messages=[
{
"role": "system",
"content": SYSTEM_PROMPT
},
{
"role": "user",
"content": f"Please analyze and provide the latest AI developments and trends for {current_date}."
}
],
model="llama3-70b-8192",
temperature=0.7,
max_tokens=2048,
top_p=0.9,
frequency_penalty=0.1,
presence_penalty=0.1
)
return response['choices'][0]['message']['content']
except Exception as e:
import traceback
print("=== Full Groq API Error ===")
print(traceback.format_exc())
print("===========================")
return "⚠️ Oops! Could not generate AI updates. Please try again later."
# Gradio app setup same as before
with gr.Blocks(title="AI Content Curator") as demo:
gr.Markdown("# πŸ“ˆ AI Content Curator")
gr.Markdown("Click to generate the latest AI developments and trends.")
with gr.Row():
with gr.Column():
submit_btn = gr.Button("πŸš€ Generate Latest AI Updates")
with gr.Column():
output = gr.Textbox(
label="πŸ”Ž AI Updates and Content Recommendations",
lines=20,
interactive=False,
placeholder="Waiting for the latest AI trends..."
)
submit_btn.click(
fn=generate_ai_updates,
inputs=[],
outputs=output
)
if __name__ == "__main__":
demo.launch(share=True)