Spaces:
Sleeping
Sleeping
Download api.py from DEADS1KE/GASIFHFS-MATH: direct link, hf CLI and curl.
- Browser
- Download file 983 Bytes
-
https://huggingface.co/spaces/DEADS1KE/GASIFHFS-MATH/resolve/main/api.py
- Command line
-
hf download hf://spaces/DEADS1KE/GASIFHFS-MATH/api.py
-
curl -L -o api.py https://huggingface.co/spaces/DEADS1KE/GASIFHFS-MATH/resolve/main/api.py
983 Bytes
| # api.py (MathSolver version) | |
| import google.generativeai as genai | |
| import streamlit as st | |
| MODEL_NAME = "gemini-2.5-pro-exp-03-25" # Hardcoded best model for math | |
| def load_model(): | |
| """Loads and caches the Gemini model.""" | |
| genai.configure(api_key=st.secrets["GOOGLE_API_KEY"]) | |
| return genai.GenerativeModel(model_name=MODEL_NAME) | |
| def start_chat_session(model): | |
| """Initiates a new chat session.""" | |
| return model.start_chat(history=[]) | |
| def generate_response(chat_session, message_content, generation_config): | |
| """Generates a response from the model.""" | |
| try: | |
| response = chat_session.send_message( | |
| message_content, | |
| stream=True, | |
| generation_config=generation_config, | |
| ) | |
| return response | |
| except genai.APIError as e: | |
| st.error(f"Gemini API Error: {e}") | |
| return None | |
| except Exception as e: | |
| st.error(f"An unexpected error occurred: {e}") | |
| return None |