|
import streamlit as st |
|
from langchain_groq import ChatGroq |
|
from langchain_core.prompts import ChatPromptTemplate |
|
from dotenv import load_dotenv |
|
import os |
|
|
|
|
|
load_dotenv() |
|
|
|
api_key = os.getenv("GROQ_API_KEY") |
|
if not api_key: |
|
raise ValueError("GROQ_API_KEY environment variable is not set.") |
|
|
|
|
|
st.set_page_config( |
|
page_title="Fun Facts Generator", |
|
page_icon="π", |
|
layout="centered" |
|
) |
|
|
|
|
|
st.title("π Jokerrr - AI Fun Facts Generator") |
|
col1, col2, col3 = st.columns([1,2,1]) |
|
with col2: |
|
st.image("img/batman.png", |
|
width=500) |
|
|
|
|
|
@st.cache_resource |
|
def initialize_llm(): |
|
return ChatGroq( |
|
model="qwen-2.5-32b", |
|
temperature=0.7, |
|
max_tokens=100, |
|
timeout=None, |
|
max_retries=2, |
|
groq_api_key=api_key, |
|
) |
|
|
|
|
|
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. |
|
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prompt = ChatPromptTemplate.from_messages([ |
|
("system", system_prompt), |
|
("human", "Generate a fascinating fun fact!"), |
|
]) |
|
|
|
|
|
if 'fact_counter' not in st.session_state: |
|
st.session_state.fact_counter = 0 |
|
|
|
|
|
main_container = st.container() |
|
|
|
with main_container: |
|
|
|
col1, col2, col3 = st.columns([1,3,1]) |
|
|
|
|
|
|
|
|
|
with col2: |
|
st.write("Click the button below to get AI-generated fun facts! π") |
|
|
|
|
|
if st.button("Generate Fun Fact! π²", use_container_width=True): |
|
try: |
|
|
|
with st.spinner("Generating a fascinating fact..."): |
|
|
|
llm = initialize_llm() |
|
response = llm.invoke(prompt.format()) |
|
|
|
|
|
st.markdown("---") |
|
st.success(response.content) |
|
|
|
|
|
st.session_state.fact_counter += 1 |
|
|
|
except Exception as e: |
|
st.error(f"Oops! Something went wrong: {str(e)}") |
|
|
|
|
|
|
|
st.markdown("---") |
|
col1, col2, col3 = st.columns([1,2,1]) |
|
with col2: |
|
st.markdown("Made with β€οΈ using Streamlit by Manith Jayaba") |
|
|