Manith Marapperuma πΎ
commited on
Commit
Β·
303d6c2
1
Parent(s):
ca25339
initial_commit
Browse files- app.py +97 -0
- requirements.txt +14 -0
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain_groq import ChatGroq
|
3 |
+
from langchain_core.prompts import ChatPromptTemplate
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Load environment variables
|
8 |
+
load_dotenv()
|
9 |
+
|
10 |
+
api_key = os.getenv("GROQ_API_KEY")
|
11 |
+
if not api_key:
|
12 |
+
raise ValueError("GROQ_API_KEY environment variable is not set.")
|
13 |
+
|
14 |
+
# Initialize the page configuration
|
15 |
+
st.set_page_config(
|
16 |
+
page_title="Fun Facts Generator",
|
17 |
+
page_icon="π",
|
18 |
+
layout="centered"
|
19 |
+
)
|
20 |
+
|
21 |
+
# Add a title to the app
|
22 |
+
st.title("π AI Fun Facts Generator")
|
23 |
+
|
24 |
+
# Initialize LLM
|
25 |
+
@st.cache_resource
|
26 |
+
def initialize_llm():
|
27 |
+
return ChatGroq(
|
28 |
+
model="mixtral-8x7b-32768", # Replace with your desired model
|
29 |
+
temperature=0.7,
|
30 |
+
max_tokens=100,
|
31 |
+
timeout=None,
|
32 |
+
max_retries=2,
|
33 |
+
groq_api_key=api_key,
|
34 |
+
)
|
35 |
+
|
36 |
+
# Create prompt template
|
37 |
+
system_prompt = """
|
38 |
+
You are a fun fact generator bot! Generate a unique, interesting, and quirky fact that most people wouldn't know.
|
39 |
+
The fact should be:
|
40 |
+
- Truly interesting and surprising
|
41 |
+
- Scientifically accurate
|
42 |
+
- Clear and concise (1-2 sentences)
|
43 |
+
- Suitable for all ages
|
44 |
+
- Not about disturbing or controversial topics
|
45 |
+
|
46 |
+
Respond with ONLY the fun fact, no additional text or context.
|
47 |
+
|
48 |
+
Example good responses:
|
49 |
+
"A day on Venus is longer than its year due to its slow rotation and faster orbit around the Sun."
|
50 |
+
"Honeybees can recognize human faces by learning patterns of facial features."
|
51 |
+
"The average cumulus cloud weighs around 1.1 million pounds due to its water content."
|
52 |
+
"""
|
53 |
+
|
54 |
+
prompt = ChatPromptTemplate.from_messages([
|
55 |
+
("system", system_prompt),
|
56 |
+
("human", "Generate a fascinating fun fact!"),
|
57 |
+
])
|
58 |
+
|
59 |
+
# Create session state for tracking
|
60 |
+
if 'fact_counter' not in st.session_state:
|
61 |
+
st.session_state.fact_counter = 0
|
62 |
+
|
63 |
+
# Main content container
|
64 |
+
main_container = st.container()
|
65 |
+
|
66 |
+
with main_container:
|
67 |
+
st.write("Click the button below to get AI-generated fun facts! π")
|
68 |
+
|
69 |
+
# Create columns for center alignment
|
70 |
+
col1, col2, col3 = st.columns([1,2,1])
|
71 |
+
|
72 |
+
# Put the button in the middle column
|
73 |
+
with col2:
|
74 |
+
if st.button("Generate Fun Fact! π²", use_container_width=True):
|
75 |
+
try:
|
76 |
+
# Show loading message
|
77 |
+
with st.spinner("Generating a fascinating fact..."):
|
78 |
+
# Initialize LLM and generate fact
|
79 |
+
llm = initialize_llm()
|
80 |
+
response = llm.invoke(prompt.format())
|
81 |
+
|
82 |
+
# Display the generated fact
|
83 |
+
st.markdown("---")
|
84 |
+
st.success(response.content)
|
85 |
+
|
86 |
+
# Increment counter
|
87 |
+
st.session_state.fact_counter += 1
|
88 |
+
|
89 |
+
except Exception as e:
|
90 |
+
st.error(f"Oops! Something went wrong: {str(e)}")
|
91 |
+
|
92 |
+
|
93 |
+
# Add a footer
|
94 |
+
st.markdown("---")
|
95 |
+
col1, col2, col3 = st.columns([1,2,1])
|
96 |
+
with col2:
|
97 |
+
st.markdown("Made with β€οΈ using Streamlit by Manith Jayaba")
|
requirements.txt
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
flask
|
2 |
+
google-generativeai
|
3 |
+
langchain
|
4 |
+
langchain-community
|
5 |
+
langchain-core
|
6 |
+
langchain-google-genai
|
7 |
+
langchain-text-splitters
|
8 |
+
faiss-cpu
|
9 |
+
pymongo
|
10 |
+
python-dotenv
|
11 |
+
nest-asyncio
|
12 |
+
motor
|
13 |
+
streamlit
|
14 |
+
langchain-groq
|