File size: 2,030 Bytes
87fea79
 
3648e3d
87fea79
3648e3d
87fea79
b7ba0d7
87fea79
 
 
 
 
 
 
 
 
 
3648e3d
2da4577
3648e3d
 
 
 
 
 
2da4577
3648e3d
2da4577
 
 
3648e3d
2da4577
 
 
 
3648e3d
2da4577
3648e3d
2da4577
 
3648e3d
87fea79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()