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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from langchain_groq import ChatGroq
|
| 3 |
+
from langchain.chains import LLMMathChain, LLMChain
|
| 4 |
+
from langchain.prompts import PromptTemplate
|
| 5 |
+
from langchain_community.utilities import WikipediaAPIWrapper
|
| 6 |
+
from langchain.agents.agent_types import AgentType
|
| 7 |
+
from langchain.agents import Tool, initialize_agent
|
| 8 |
+
from langchain.callbacks import StreamlitCallbackHandler
|
| 9 |
+
import os
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
|
| 12 |
+
# Load environment variables
|
| 13 |
+
load_dotenv()
|
| 14 |
+
|
| 15 |
+
# Streamlit page configuration
|
| 16 |
+
st.set_page_config(
|
| 17 |
+
page_title="AI Math Problem Solver & Research Assistant",
|
| 18 |
+
page_icon="๐งฎ",
|
| 19 |
+
layout="wide"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Custom CSS styling
|
| 23 |
+
st.markdown("""
|
| 24 |
+
<style>
|
| 25 |
+
.main {
|
| 26 |
+
background-color: #f5f5f5;
|
| 27 |
+
}
|
| 28 |
+
.stTitle {
|
| 29 |
+
color: #1e3d59;
|
| 30 |
+
font-size: 2.5rem !important;
|
| 31 |
+
font-weight: 700 !important;
|
| 32 |
+
padding-bottom: 1rem;
|
| 33 |
+
}
|
| 34 |
+
.stTextArea textarea {
|
| 35 |
+
background-color: #ffffff;
|
| 36 |
+
border-radius: 10px;
|
| 37 |
+
border: 1px solid #e0e0e0;
|
| 38 |
+
padding: 10px;
|
| 39 |
+
}
|
| 40 |
+
.stButton button {
|
| 41 |
+
background-color: #17b794;
|
| 42 |
+
color: white;
|
| 43 |
+
border-radius: 20px;
|
| 44 |
+
padding: 0.5rem 2rem;
|
| 45 |
+
font-weight: 600;
|
| 46 |
+
}
|
| 47 |
+
.stButton button:hover {
|
| 48 |
+
background-color: #148f77;
|
| 49 |
+
}
|
| 50 |
+
div.stSpinner > div {
|
| 51 |
+
border-top-color: #17b794 !important;
|
| 52 |
+
}
|
| 53 |
+
</style>
|
| 54 |
+
""", unsafe_allow_html=True)
|
| 55 |
+
|
| 56 |
+
# App Header
|
| 57 |
+
col1, col2, col3 = st.columns([1,6,1])
|
| 58 |
+
with col2:
|
| 59 |
+
st.title("๐งฎ AI Math Problem Solver & Research Assistant")
|
| 60 |
+
st.markdown("""
|
| 61 |
+
<div style='background-color: #ffffff; padding: 1rem; border-radius: 10px; margin-bottom: 2rem;'>
|
| 62 |
+
<p style='color: #666666; margin-bottom: 0;'>
|
| 63 |
+
Powered by Google Gemma 2 AI, this assistant can help you solve math problems,
|
| 64 |
+
provide detailed explanations, and search for additional information.
|
| 65 |
+
</p>
|
| 66 |
+
</div>
|
| 67 |
+
""", unsafe_allow_html=True)
|
| 68 |
+
|
| 69 |
+
# API Key Check
|
| 70 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 71 |
+
if not groq_api_key:
|
| 72 |
+
st.error("โ ๏ธ Please add your Groq API key to continue")
|
| 73 |
+
st.stop()
|
| 74 |
+
|
| 75 |
+
# Initialize LLM
|
| 76 |
+
llm = ChatGroq(model="gemma2-9b-it", groq_api_key=groq_api_key)
|
| 77 |
+
|
| 78 |
+
# Tool Setup
|
| 79 |
+
wikipedia_wrapper = WikipediaAPIWrapper()
|
| 80 |
+
wikipedia_tool = Tool(
|
| 81 |
+
name="Wikipedia",
|
| 82 |
+
func=wikipedia_wrapper.run,
|
| 83 |
+
description="A tool for searching the Internet to find various information on the topics mentioned"
|
| 84 |
+
)
|
| 85 |
+
def safe_calculator(expression: str) -> str:
|
| 86 |
+
try:
|
| 87 |
+
# Clean and validate the expression
|
| 88 |
+
if any(char in expression for char in ['โซ', 'โ', 'โ']):
|
| 89 |
+
return "I apologize, but I cannot directly solve calculus problems or complex mathematical expressions. I can help explain the steps to solve it though!"
|
| 90 |
+
|
| 91 |
+
# Use the math chain
|
| 92 |
+
result = math_chain.run(expression)
|
| 93 |
+
return result
|
| 94 |
+
except Exception as e:
|
| 95 |
+
return f"I encountered an error trying to solve this mathematically. Let me help explain the steps to solve it instead."
|
| 96 |
+
|
| 97 |
+
math_chain = LLMMathChain.from_llm(llm=llm,verbose=True,input_key="question",output_key="answer")
|
| 98 |
+
calculator = Tool(
|
| 99 |
+
name="Calculator",
|
| 100 |
+
func=safe_calculator,
|
| 101 |
+
description="A tool for solving basic mathematical expressions. For complex math, it will provide step-by-step explanations"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
prompt = """
|
| 105 |
+
You're a helpful math tutor tasked with solving mathematical questions. For each problem:
|
| 106 |
+
1. First determine if it's a basic arithmetic problem or a more complex mathematical problem
|
| 107 |
+
2. For basic arithmetic, use the calculator tool
|
| 108 |
+
3. For complex math (calculus, integrals, differential equations), explain the solution steps clearly
|
| 109 |
+
4. Always show your work and explain each step
|
| 110 |
+
|
| 111 |
+
Question: {question}
|
| 112 |
+
|
| 113 |
+
Let me solve this step by step:
|
| 114 |
+
"""
|
| 115 |
+
|
| 116 |
+
prompt_template = PromptTemplate(
|
| 117 |
+
input_variables=["question"],
|
| 118 |
+
template=prompt
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
chain = LLMChain(llm=llm, prompt=prompt_template)
|
| 122 |
+
reasoning_tool = Tool(
|
| 123 |
+
name="Reasoning tool",
|
| 124 |
+
func=chain.run,
|
| 125 |
+
description="A tool for answering logic-based and reasoning questions."
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Initialize Agent
|
| 129 |
+
assistant_agent = initialize_agent(
|
| 130 |
+
tools=[wikipedia_tool, calculator, reasoning_tool],
|
| 131 |
+
llm=llm,
|
| 132 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
| 133 |
+
verbose=False,
|
| 134 |
+
handle_parsing_errors=True
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
# Chat History
|
| 138 |
+
if "messages" not in st.session_state:
|
| 139 |
+
st.session_state["messages"] = [
|
| 140 |
+
{"role": "assistant", "content": "๐ Hi! I'm your Math Assistant. I can help you solve math problems and provide detailed explanations."}
|
| 141 |
+
]
|
| 142 |
+
|
| 143 |
+
# Display Chat History
|
| 144 |
+
for msg in st.session_state.messages:
|
| 145 |
+
with st.chat_message(msg["role"]):
|
| 146 |
+
st.write(msg["content"])
|
| 147 |
+
|
| 148 |
+
# Input Section
|
| 149 |
+
st.markdown("### ๐ Your Question")
|
| 150 |
+
question = st.text_area(
|
| 151 |
+
label="Enter your question:",
|
| 152 |
+
value="I have 5 bananas and 7 grapes. I eat 2 bananas and give away 3 grapes. Then I buy a dozen apples and 2 packs of blueberries. Each pack of blueberries contains 25 berries. How many total pieces of fruit do I have at the end?",
|
| 153 |
+
label_visibility="collapsed",
|
| 154 |
+
height=100
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# Create two columns for button centering
|
| 158 |
+
col1, col2, col3 = st.columns([2,1,2])
|
| 159 |
+
with col2:
|
| 160 |
+
solve_button = st.button("๐ Solve Problem")
|
| 161 |
+
|
| 162 |
+
if solve_button:
|
| 163 |
+
if question:
|
| 164 |
+
with st.spinner("๐ค Thinking..."):
|
| 165 |
+
st.session_state.messages.append({"role": "user", "content": question})
|
| 166 |
+
with st.chat_message("user"):
|
| 167 |
+
st.write(question)
|
| 168 |
+
|
| 169 |
+
st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
|
| 170 |
+
response = assistant_agent.run(st.session_state.messages, callbacks=[st_cb])
|
| 171 |
+
|
| 172 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 173 |
+
|
| 174 |
+
st.markdown("### ๐ก Solution:")
|
| 175 |
+
with st.chat_message("assistant"):
|
| 176 |
+
st.success(response)
|
| 177 |
+
else:
|
| 178 |
+
st.warning("โ ๏ธ Please enter your question first!")
|
| 179 |
+
|
| 180 |
+
# Footer
|
| 181 |
+
st.markdown("""
|
| 182 |
+
<div style='position: fixed; bottom: 0; left: 0; width: 100%; background-color: #f0f2f6; padding: 1rem; text-align: center;'>
|
| 183 |
+
<p style='color: #666666; margin-bottom: 0;'>
|
| 184 |
+
Made with โค๏ธ using Streamlit and Google Gemma 2
|
| 185 |
+
</p>
|
| 186 |
+
</div>
|
| 187 |
+
""", unsafe_allow_html=True)
|
requirements.txt
ADDED
|
Binary file (458 Bytes). View file
|
|
|