setiyanikhil3's picture
Update app.py
c2f59fe verified
raw
history blame
2.61 kB
import os
import gradio as gr
from groq import Groq
from dotenv import load_dotenv
import base64
from datetime import datetime
# Load environment variables
load_dotenv()
# Initialize Groq client
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
def load_system_prompt():
"""Load and decode the system prompt from the config file"""
try:
# Get the directory of the current script
current_dir = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(current_dir, "config", "system_prompt.txt")
# Read the system prompt
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 the system prompt once when the app starts
SYSTEM_PROMPT = load_system_prompt()
def generate_ai_updates():
"""Generate AI updates using Groq API"""
try:
# Get current date for context
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 in the system prompt."
}
],
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:
return f"Error: {str(e)}"
# Create Gradio interface
with gr.Blocks(title="AI Content Curator") as demo:
gr.Markdown("# AI Content Curator")
gr.Markdown("Click the button below to get the latest AI developments and content 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
)
submit_btn.click(
fn=generate_ai_updates,
inputs=[],
outputs=output
)
if __name__ == "__main__":
demo.launch(share=True)