Skip to content

Commit

Permalink
Key fix...
Browse files Browse the repository at this point in the history
  • Loading branch information
jgravelle committed Jul 2, 2024
1 parent 83e142c commit 1446d75
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
22 changes: 15 additions & 7 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import streamlit as st
from src.ui.streamlit_ui import TokenMyzerUI
from src.api.groq_api import GroqAPI
from config.config import load_config

def main():
st.set_page_config(page_title="TokenMyzer", page_icon="🔤", layout="wide")

config = load_config()
api = GroqAPI(config)
if 'api' not in st.session_state:
st.session_state.api = GroqAPI()

if not st.session_state.api.client or not st.session_state.get('api_key_valid', False):
st.warning("Please enter a valid Groq API key to use the app.")
st.session_state.api.initialize_client()

if api.client:
ui = TokenMyzerUI(api)
if st.session_state.api.client and st.session_state.get('api_key_valid', False):
ui = TokenMyzerUI(st.session_state.api)
ui.run()
else:
st.error("Failed to initialize the Groq API client. Please check your API key and try again.")
elif st.session_state.get('api_key_valid') == False:
st.error("The provided API key is invalid. Please enter a valid Groq API key.")
# Add a button to clear the invalid key
if st.button("Clear API Key"):
st.session_state.pop('groq_api_key', None)
st.session_state.pop('api_key_valid', None)
st.experimental_rerun()

if __name__ == "__main__":
main()
39 changes: 19 additions & 20 deletions src/api/groq_api.py
Original file line number Diff line number Diff line change
@@ -1,52 +1,51 @@
# src/api/groq_api.py

import streamlit as st
import requests
from groq import Groq
import os
from dotenv import load_dotenv

class GroqAPI:
def __init__(self, config):
self.config = config
def __init__(self):
self.client = None
self.initialize_client()

def initialize_client(self):
api_key = self.get_api_key()
if api_key:
self.client = Groq(api_key=api_key)
self.config['api_key'] = api_key
else:
st.error("No API key provided. The app cannot function without a valid Groq API key.")
st.stop()
self.test_api_key() # Test the API key

def get_api_key(self):
# Try to get the API key from environment variables
load_dotenv() # This loads the .env file if it exists
load_dotenv()
api_key = os.getenv("GROQ_API_KEY")

# If not found in environment, prompt the user
# If not found in environment, check session state
if not api_key and 'groq_api_key' in st.session_state:
api_key = st.session_state.groq_api_key

# If still not found, prompt the user
if not api_key:
st.warning("GROQ_API_KEY not found in environment variables.")
api_key = st.text_input("Please enter your Groq API key:", type="password")
api_key = st.text_input("Please enter your Groq API key:", type="password", key="api_key_input")
if api_key:
# Optionally, you can save this to .env file for future use
with open(".env", "a") as env_file:
env_file.write(f"\nGROQ_API_KEY={api_key}")
st.success("API key saved for this session. Restart the app to use the saved key.")
st.session_state.groq_api_key = api_key

return api_key

def test_api_key(self):
try:
self.get_models()
st.session_state.api_key_valid = True
except Exception:
st.session_state.api_key_valid = False
self.client = None # Reset client if key is invalid

def get_models(self):
if not self.client:
return None

headers = {
"Authorization": f"Bearer {self.config['api_key']}",
"Content-Type": "application/json"
}
response = requests.get(self.config['api_url'], headers=headers)
response = self.client.models.list()
return response.json()

def chat_completion(self, model, user_input):
Expand Down

0 comments on commit 1446d75

Please sign in to comment.