Spaces:
Build error
Build error
Download api_utils.py from schennu/SeniorMatch2.0: direct link, hf CLI and curl.
- Browser
- Download file 1.43 kB
-
https://huggingface.co/spaces/schennu/SeniorMatch2.0/resolve/main/api_utils.py
- Command line
-
hf download hf://spaces/schennu/SeniorMatch2.0/api_utils.py
-
curl -L -o api_utils.py https://huggingface.co/spaces/schennu/SeniorMatch2.0/resolve/main/api_utils.py
1.43 kB
| import streamlit as st | |
| import os | |
| from openai import OpenAI | |
| def initialize_session_state(): | |
| """Initialize all session state variables.""" | |
| if 'knowledge_base' not in st.session_state: | |
| st.session_state.knowledge_base = None | |
| if 'knowledge_files' not in st.session_state: | |
| st.session_state.knowledge_files = [] | |
| if 'user_api_key' not in st.session_state: | |
| st.session_state.user_api_key = '' | |
| if 'serper_api_key' not in st.session_state: | |
| st.session_state.serper_api_key = '' | |
| if 'youtube_api_key' not in st.session_state: | |
| st.session_state.youtube_api_key = '' | |
| if 'url_list' not in st.session_state: | |
| st.session_state.url_list = [] | |
| if 'api_error' not in st.session_state: | |
| st.session_state.api_error = False | |
| def open_ai_chat(user_prompt, api_key): | |
| """Handle OpenAI chat completions.""" | |
| os.environ["OPENAI_API_KEY"] = api_key | |
| client = OpenAI(api_key=api_key) | |
| try: | |
| response = client.chat.completions.create( | |
| model="gpt-3.5-turbo", | |
| messages=[ | |
| {"role": "system", "content": "You are a helpful assistant."}, | |
| {"role": "user", "content": user_prompt} | |
| ] | |
| ) | |
| return response.choices[0].message.content | |
| except Exception as e: | |
| st.error(f"OpenAI API Error: {str(e)}") | |
| return "Could not generate additional tips. Please check your API key." |