File size: 3,204 Bytes
303d6c2 b45e823 ddf9c28 39bc058 ddf9c28 303d6c2 b93b53b 303d6c2 afb3dbb 303d6c2 ddf9c28 39bc058 303d6c2 ddf9c28 39bc058 303d6c2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
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("<h6 style='text-align: center; font-family: Source Sans Pro;'>Click the button below to get AI-generated fun facts! π</h6>", 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")
|