eassylearnmath / app.py
ghaffarmumtat123's picture
Update app.py
b7ba0d7 verified
import streamlit as st
import requests
from requests.exceptions import ConnectionError, Timeout, RequestException
# GROQ API URL and API key
api_url = "https://api.groq.ai/v1/solve"
api_key = "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" # New API Key
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
# Function to solve math problem using GROQ API
def solve_math_problem(problem):
payload = {"input": problem}
try:
# Make a request to the API
response = requests.post(api_url, json=payload, headers=headers)
# Check if the response is successful
if response.status_code == 200:
return response.json().get('solution', "Solution not found.")
else:
return f"API Error: {response.status_code} - {response.text}"
except ConnectionError as e:
# Log the connection error
st.error(f"Connection error: {str(e)}")
return "Error: Unable to connect to the API. Please check your network connection or try again later."
except Timeout as e:
# Log the timeout error
st.error(f"Timeout error: {str(e)}")
return "Error: The request timed out. Please try again later."
except RequestException as e:
# Log any other requests-related errors
st.error(f"Request exception: {str(e)}")
return f"Error: An unexpected error occurred - {str(e)}"
# Streamlit user interface
def main():
st.title("Math Solution App")
st.write("Enter a math problem, and the app will solve it using the GROQ API!")
# Input box for math problem
problem = st.text_input("Enter math problem:")
if st.button("Solve"):
if problem:
with st.spinner("Solving..."):
solution = solve_math_problem(problem)
st.write(f"Solution: {solution}")
else:
st.warning("Please enter a math problem.")
# Run the app
if __name__ == "__main__":
main()