mdakhras commited on
Commit
1e5d8cf
·
verified ·
1 Parent(s): 9fd18ae

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +44 -29
src/streamlit_app.py CHANGED
@@ -1,8 +1,11 @@
1
  import streamlit as st
2
- import fitz # PyMuPDF
 
3
  from dotenv import load_dotenv
 
4
  import os
5
- import requests
 
6
 
7
  # Load environment variables from .env file
8
  load_dotenv()
@@ -11,26 +14,21 @@ load_dotenv()
11
  ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
12
  API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
13
  DEPLOYMENT_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME")
14
- azure_openai_embedding_model = os.getenv("AZURE_OPENAI_EMBEDDING_MODEL")
15
- HuggingFace_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
16
-
17
- # Set custom cache and config directory for Streamlit to avoid permission issues
18
- os.environ["STREAMLIT_CACHE_DIR"] = os.path.join(os.getcwd(), ".streamlit")
19
- os.environ["STREAMLIT_CONFIG_DIR"] = os.path.join(os.getcwd(), ".streamlit")
20
 
21
  # Check if the necessary environment variables are loaded
22
  if not API_KEY or not ENDPOINT or not DEPLOYMENT_NAME:
23
  st.error("Azure OpenAI credentials are missing. Please check your .env file.")
24
  st.stop()
25
 
26
- # Hugging Face API URL
27
- API_URL = "https://api-inference.huggingface.co/models/gpt2" # Replace with your desired model's API URL
28
- headers = {
29
- "Authorization": f"Bearer {HuggingFace_API_KEY}" # Replace with your actual API key
30
- }
31
 
32
- # Function to extract relevant information from the CV using Hugging Face or Azure OpenAI
33
- def extract_info_from_openai(text):
34
  prompt = f"""
35
  Extract the following information from this CV text:
36
  1. Job title
@@ -39,22 +37,36 @@ def extract_info_from_openai(text):
39
  4. Years of experience
40
  5. Education level
41
 
42
- Text:
43
  {text}
44
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- data = {"inputs": prompt}
47
- response = requests.post(API_URL, headers=headers, json=data)
48
 
49
- # If the Hugging Face response is successful, extract the generated text
50
- if response.status_code == 200:
51
- result = response.json() # Parse the JSON response
52
- return result.get("generated_text", "Error: Unable to generate response.")
53
- else:
54
- return f"Error: {response.status_code} - {response.text}"
55
 
56
  # Streamlit App UI
57
- st.title("AI Screening")
58
  st.write("Enter the CV text below, and the app will extract relevant information such as job title, location, skills, experience, and education.")
59
 
60
  # Text area for entering CV content manually
@@ -65,9 +77,12 @@ if cv_text:
65
  st.subheader("Entered Text from CV")
66
  st.text_area("CV Text", cv_text, height=300)
67
 
68
- # Extract relevant information using Hugging Face (or Azure OpenAI if you need)
69
- extracted_info = extract_info_from_openai(cv_text)
70
 
71
  # Display the extracted information
72
- st.subheader("Extracted Information")
73
- st.write(extracted_info)
 
 
 
 
1
  import streamlit as st
2
+ import os
3
+ import openai
4
  from dotenv import load_dotenv
5
+
6
  import os
7
+ from openai import AzureOpenAI
8
+ from azure.core.credentials import AzureKeyCredential
9
 
10
  # Load environment variables from .env file
11
  load_dotenv()
 
14
  ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
15
  API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
16
  DEPLOYMENT_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME")
17
+ API_VERSION = os.getenv("AZURE_OPENAI_API_VERION")
 
 
 
 
 
18
 
19
  # Check if the necessary environment variables are loaded
20
  if not API_KEY or not ENDPOINT or not DEPLOYMENT_NAME:
21
  st.error("Azure OpenAI credentials are missing. Please check your .env file.")
22
  st.stop()
23
 
24
+ client = AzureOpenAI(
25
+ api_version=API_VERSION,
26
+ azure_endpoint=ENDPOINT,
27
+ api_key=API_KEY,
28
+ )
29
 
30
+ # Function to extract information from CV using Azure OpenAI GPT-4 model
31
+ def extract_info_from_azure(text):
32
  prompt = f"""
33
  Extract the following information from this CV text:
34
  1. Job title
 
37
  4. Years of experience
38
  5. Education level
39
 
40
+ CV Text:
41
  {text}
42
  """
43
+ response = client.chat.completions.create(
44
+ messages=[
45
+ {
46
+ "role": "system",
47
+ "content": "You are a helpful assistant.",
48
+ },
49
+ {
50
+ "role": "user",
51
+ "content": prompt,
52
+ }
53
+ ],
54
+ max_tokens=4096,
55
+ temperature=1.0,
56
+ top_p=1.0,
57
+ model=DEPLOYMENT_NAME
58
+ )
59
 
60
+ try:
 
61
 
62
+ result = response.choices[0].message.content
63
+ return result
64
+ except Exception as e:
65
+ st.error(f"Error occurred while contacting Azure OpenAI: {e}")
66
+ return None
 
67
 
68
  # Streamlit App UI
69
+ st.title("AI Screening for CV Information Extraction")
70
  st.write("Enter the CV text below, and the app will extract relevant information such as job title, location, skills, experience, and education.")
71
 
72
  # Text area for entering CV content manually
 
77
  st.subheader("Entered Text from CV")
78
  st.text_area("CV Text", cv_text, height=300)
79
 
80
+ # Extract relevant information using Azure OpenAI GPT-4
81
+ extracted_info = extract_info_from_azure(cv_text)
82
 
83
  # Display the extracted information
84
+ if extracted_info:
85
+ st.subheader("Extracted Information")
86
+ st.write(extracted_info)
87
+ else:
88
+ st.write("Could not extract information. Please try again.")