Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +44 -29
src/streamlit_app.py
CHANGED
|
@@ -1,8 +1,11 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
|
|
|
| 3 |
from dotenv import load_dotenv
|
|
|
|
| 4 |
import os
|
| 5 |
-
import
|
|
|
|
| 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 |
-
|
| 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 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
|
| 32 |
-
# Function to extract
|
| 33 |
-
def
|
| 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 |
-
|
| 47 |
-
response = requests.post(API_URL, headers=headers, json=data)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 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
|
| 69 |
-
extracted_info =
|
| 70 |
|
| 71 |
# Display the extracted information
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 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.")
|