import streamlit as st from langchain_groq import ChatGroq from langchain_core.prompts import ChatPromptTemplate from dotenv import load_dotenv import os # Load environment variables load_dotenv() api_key = os.getenv("GROQ_API_KEY") if not api_key: raise ValueError("GROQ_API_KEY environment variable is not set.") # Initialize the page configuration st.set_page_config( page_title="Fun Facts Generator", page_icon="🎈", layout="centered" ) # Add a title to the app st.title("🎈 Jokerrr - AI Fun Facts Generator") col1, col2, col3 = st.columns([1,12,1]) with col2: st.image("img/batman.png", width=600) # Initialize LLM @st.cache_resource def initialize_llm(): return ChatGroq( model="qwen-2.5-32b", # Replace with your desired model temperature=0.7, max_tokens=100, timeout=None, max_retries=2, groq_api_key=api_key, ) # Create prompt template system_prompt = """ You are a fun fact generator bot! Generate a unique, interesting, and quirky fact that most people wouldn't know. The fact should be: - Truly interesting and surprising - Scientifically accurate - Clear and concise (1-2 sentences) - Suitable for all ages - Not about disturbing or controversial topics Respond with ONLY the fun fact, no additional text or context. """ # Example good responses: # "A day on Venus is longer than its year due to its slow rotation and faster orbit around the Sun." # "Honeybees can recognize human faces by learning patterns of facial features." # "The average cumulus cloud weighs around 1.1 million pounds due to its water content." prompt = ChatPromptTemplate.from_messages([ ("system", system_prompt), ("human", "Generate a fascinating fun fact!"), ]) # Create session state for tracking if 'fact_counter' not in st.session_state: st.session_state.fact_counter = 0 # Main content container main_container = st.container() with main_container: # Create columns for center alignment col1, col2, col3 = st.columns([1,8,1]) # Put the button in the middle column with col2: st.markdown("
Click the button below to get AI-generated fun facts! 🎉
", unsafe_allow_html=True) if st.button("Generate Fun Fact! 🎲", use_container_width=True): try: # Show loading message with st.spinner("Generating a fascinating fact..."): # Initialize LLM and generate fact llm = initialize_llm() response = llm.invoke(prompt.format()) # Display the generated fact st.markdown("---") st.success(response.content) # Increment counter st.session_state.fact_counter += 1 except Exception as e: st.error(f"Oops! Something went wrong: {str(e)}") # Add a footer st.markdown("---") col1, col2, col3 = st.columns([1,2,1]) with col2: st.markdown("Made with ❤️ using Streamlit by Manith Jayaba")