Sharan1712 commited on
Commit
976096b
Β·
1 Parent(s): b7c271c
Files changed (2) hide show
  1. app.py +15 -10
  2. utils/utils.py +52 -1
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import streamlit as st
 
2
  from utils.constants import model_family_mapping, model_name_mapping
3
  from utils.utils import PitchPerfect, pdf_loader
4
 
@@ -41,16 +42,20 @@ with st.sidebar:
41
  token = st.text_input("OpenAI API Key", type="password", key="openai_key")
42
  else:
43
  token = st.text_input("Hugging Face Token", type="password", key="hf_token")
44
-
45
- if st.button("Initialize with the provided keys"):
46
- try:
47
- st.session_state.pitch_perfect = PitchPerfect(model = model_name, model_family = model_family, token = token)
48
- st.session_state.api_configured = True
49
- st.success("Successfully configured the API clients with provided keys!")
50
-
51
- except Exception as e:
52
- st.error(f"Error initializing API clients: {str(e)}")
53
- st.session_state.api_configured = False
 
 
 
 
54
 
55
  if st.session_state.api_configured:
56
  upload_cv = st.file_uploader("Upload CV in PDF format", type=["pdf"])
 
1
  import streamlit as st
2
+ import openai
3
  from utils.constants import model_family_mapping, model_name_mapping
4
  from utils.utils import PitchPerfect, pdf_loader
5
 
 
42
  token = st.text_input("OpenAI API Key", type="password", key="openai_key")
43
  else:
44
  token = st.text_input("Hugging Face Token", type="password", key="hf_token")
45
+
46
+ if token != "":
47
+ if st.button("Initialize with the provided keys"):
48
+ try:
49
+ st.session_state.pitch_perfect = PitchPerfect(model = model_name, model_family = model_family, token = token)
50
+ if st.session_state.pitch_perfect.client == "INVALID":
51
+ st.error(st.session_state.pitch_perfect.error)
52
+ else:
53
+ st.session_state.api_configured = True
54
+ st.success("Successfully configured the API clients with provided keys!")
55
+
56
+ except Exception as e:
57
+ st.error(f"Error initializing API clients: {str(e)}")
58
+ st.session_state.api_configured = False
59
 
60
  if st.session_state.api_configured:
61
  upload_cv = st.file_uploader("Upload CV in PDF format", type=["pdf"])
utils/utils.py CHANGED
@@ -1,5 +1,7 @@
1
  from langchain_community.document_loaders import PyPDFLoader
2
  from huggingface_hub import InferenceClient
 
 
3
  from openai import OpenAI
4
  from utils.constants import system_prompt
5
 
@@ -22,12 +24,61 @@ class PitchPerfect:
22
 
23
  if model_family == "gpt":
24
  self.client = OpenAI(api_key = token)
 
25
  elif model_family == "together":
26
  self.client = InferenceClient(provider = "together", api_key = token)
 
27
  else:
28
  self.client = InferenceClient(provider="hf-inference", api_key = token)
 
29
 
30
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  def prepare_user_prompt(self, job_title, company, job_desc, cv_data, word_limit):
33
 
 
1
  from langchain_community.document_loaders import PyPDFLoader
2
  from huggingface_hub import InferenceClient
3
+ import requests
4
+ import openai
5
  from openai import OpenAI
6
  from utils.constants import system_prompt
7
 
 
24
 
25
  if model_family == "gpt":
26
  self.client = OpenAI(api_key = token)
27
+ self.check_openai_token()
28
  elif model_family == "together":
29
  self.client = InferenceClient(provider = "together", api_key = token)
30
+ self.check_hf_token(token)
31
  else:
32
  self.client = InferenceClient(provider="hf-inference", api_key = token)
33
+ self.check_hf_token(token)
34
 
35
+ def check_openai_token(self):
36
+ try:
37
+ self.client.models.list()
38
+ except openai.AuthenticationError:
39
+ print("❌ Invalid API key.")
40
+ self.client = "INVALID"
41
+ self.error = "❌ Invalid API key."
42
+ except Exception as e:
43
+ print(f"❌ An error occurred: {e}")
44
+ self.client = "INVALID"
45
+ self.error = "❌ An error occurred: {e}"
46
+
47
+ def check_hf_token(self, token):
48
+ headers = {"Authorization": f"Bearer {token}"}
49
+ try:
50
+ response = requests.get("https://huggingface.co/api/whoami", headers=headers)
51
+ if response.status_code == 200:
52
+ user_info = response.json()
53
+ print(f"βœ… Token is valid. Logged in as: {user_info.get('name', 'Unknown User')}")
54
+ elif response.status_code == 401:
55
+ print("❌ Invalid Hugging Face token.")
56
+ self.client = "INVALID"
57
+ self.error = "❌ Invalid HF Token."
58
+ elif response.status_code == 403:
59
+ print("❌ Token does not have the necessary permissions.")
60
+ self.client = "INVALID"
61
+ self.error = "❌ Token does not have the necessary permissions."
62
+ else:
63
+ print(f"❌ Unexpected error: {response.status_code} - {response.text}")
64
+ self.client = "INVALID"
65
+ self.error = f"❌ Unexpected error: {response.status_code} - {response.text}"
66
+ except Exception as e:
67
+ print(f"❌ An unexpected error occurred: {e}")
68
+ self.client = "INVALID"
69
+ self.error = f"❌ An unexpected error occurred: {e}"
70
+
71
+ # def check_api_key(self):
72
+ # try:
73
+ # # Make a simple request to test the API key
74
+ # self.client.models.list()
75
+ # print("βœ… API key is valid.")
76
+ # except openai.APIConnectionError:
77
+ # print("❌ Network error. Please check your connection.")
78
+ # except openai.AuthenticationError:
79
+ # print("❌ Invalid API key.")
80
+ # except Exception as e:
81
+ # print(f"❌ An error occurred: {e}")
82
 
83
  def prepare_user_prompt(self, job_title, company, job_desc, cv_data, word_limit):
84