import os import gradio as gr from groq import Groq from dotenv import load_dotenv from datetime import datetime # Load environment variables load_dotenv() # Initialize Groq client properly client = Groq(api_key=os.getenv("GROQ_API_KEY")) def load_system_prompt(): """Load and decode the system prompt from the config file.""" 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." # Load system prompt once SYSTEM_PROMPT = load_system_prompt() def generate_ai_updates(): """Generate AI updates using Groq API.""" try: current_date = datetime.now().strftime("%B %d, %Y") chat_completion = client.chat.completions.create( messages=[ { "role": "system", "content": SYSTEM_PROMPT }, { "role": "user", "content": f"Please analyze and provide the latest AI developments and trends for {current_date}. Follow the verification workflow and create content as specified." } ], model="llama3-70b-8192", temperature=0.7, max_tokens=2048, top_p=0.9, frequency_penalty=0.1, presence_penalty=0.1 ) return chat_completion.choices[0].message.content except Exception as e: import traceback print("=== Groq API Error ===") print(traceback.format_exc()) print("======================") return "⚠️ Oops! Could not generate AI updates. Please try again later." # Gradio app with gr.Blocks(title="AI Content Curator") as demo: gr.Markdown("# 📈 AI Content Curator") gr.Markdown("Click below to generate the latest AI developments and recommendations.") 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 AI updates..." ) submit_btn.click( fn=generate_ai_updates, inputs=[], outputs=output ) if __name__ == "__main__": demo.launch(share=True)