Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +21 -42
src/streamlit_app.py
CHANGED
|
@@ -4,40 +4,33 @@ from dotenv import load_dotenv
|
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
import requests
|
|
|
|
| 7 |
# Load environment variables from .env file
|
| 8 |
load_dotenv()
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
# from langchain_openai import AzureChatOpenAI
|
| 12 |
-
|
| 13 |
-
# Set custom cache and config directory for Streamlit to avoid permission issues
|
| 14 |
-
os.environ["STREAMLIT_CACHE_DIR"] = os.path.join(os.getcwd(), ".streamlit")
|
| 15 |
-
os.environ["STREAMLIT_CONFIG_DIR"] = os.path.join(os.getcwd(), ".streamlit")
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
#set env
|
| 19 |
ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
|
| 20 |
API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
|
| 21 |
DEPLOYMENT_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME")
|
| 22 |
-
azure_openai_embedding_model
|
| 23 |
HuggingFace_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
# Check if the necessary environment variables are loaded
|
| 29 |
if not API_KEY or not ENDPOINT or not DEPLOYMENT_NAME:
|
| 30 |
st.error("Azure OpenAI credentials are missing. Please check your .env file.")
|
| 31 |
st.stop()
|
| 32 |
|
| 33 |
-
#
|
| 34 |
API_URL = "https://api-inference.huggingface.co/models/gpt2" # Replace with your desired model's API URL
|
| 35 |
headers = {
|
| 36 |
"Authorization": f"Bearer {HuggingFace_API_KEY}" # Replace with your actual API key
|
| 37 |
}
|
| 38 |
|
| 39 |
-
|
| 40 |
-
# Function to extract text from the PDF
|
| 41 |
def extract_text_from_pdf(pdf_file):
|
| 42 |
# Read the uploaded file as a byte stream
|
| 43 |
pdf_bytes = pdf_file.read()
|
|
@@ -49,9 +42,7 @@ def extract_text_from_pdf(pdf_file):
|
|
| 49 |
text += page.get_text()
|
| 50 |
return text
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# Function to extract relevant information from the CV using Azure OpenAI (ChatGPT)
|
| 55 |
def extract_info_from_openai(text):
|
| 56 |
prompt = f"""
|
| 57 |
Extract the following information from this CV text:
|
|
@@ -65,46 +56,34 @@ def extract_info_from_openai(text):
|
|
| 65 |
{text}
|
| 66 |
"""
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
# data = {"inputs": "Hello, Hugging Face!"}
|
| 72 |
data = {"inputs": prompt}
|
| 73 |
response = requests.post(API_URL, headers=headers, json=data)
|
| 74 |
-
|
| 75 |
-
# response
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
# )
|
| 84 |
-
|
| 85 |
-
# Parse the AI response
|
| 86 |
-
result = response.text #.json() #response.result
|
| 87 |
-
return result #result.choices[0].text.strip()
|
| 88 |
-
|
| 89 |
-
# Streamlit App
|
| 90 |
st.title("AI Screening")
|
| 91 |
-
st.title("CV Information Extractor with Azure OpenAI (GPT-4)")
|
| 92 |
st.write("Upload a CV PDF file, and the app will extract relevant information such as job title, location, skills, experience, and education.")
|
| 93 |
|
| 94 |
# File uploader
|
| 95 |
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
|
| 96 |
|
| 97 |
if uploaded_file is not None:
|
| 98 |
-
# Extract text from PDF
|
| 99 |
text = extract_text_from_pdf(uploaded_file)
|
| 100 |
|
| 101 |
# Display the extracted text (optional)
|
| 102 |
st.subheader("Extracted Text from CV")
|
| 103 |
st.text_area("Text from CV", text, height=300)
|
| 104 |
|
| 105 |
-
# Extract relevant
|
| 106 |
extracted_info = extract_info_from_openai(text)
|
| 107 |
|
| 108 |
# Display the extracted information
|
| 109 |
st.subheader("Extracted Information")
|
| 110 |
-
st.write(extracted_info)
|
|
|
|
| 4 |
import io
|
| 5 |
import os
|
| 6 |
import requests
|
| 7 |
+
|
| 8 |
# Load environment variables from .env file
|
| 9 |
load_dotenv()
|
| 10 |
|
| 11 |
+
# Check and set environment variables
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
|
| 13 |
API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
|
| 14 |
DEPLOYMENT_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME")
|
| 15 |
+
azure_openai_embedding_model = os.getenv("AZURE_OPENAI_EMBEDDING_MODEL")
|
| 16 |
HuggingFace_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
| 17 |
|
| 18 |
+
# Set custom cache and config directory for Streamlit to avoid permission issues
|
| 19 |
+
os.environ["STREAMLIT_CACHE_DIR"] = os.path.join(os.getcwd(), ".streamlit")
|
| 20 |
+
os.environ["STREAMLIT_CONFIG_DIR"] = os.path.join(os.getcwd(), ".streamlit")
|
| 21 |
|
| 22 |
# Check if the necessary environment variables are loaded
|
| 23 |
if not API_KEY or not ENDPOINT or not DEPLOYMENT_NAME:
|
| 24 |
st.error("Azure OpenAI credentials are missing. Please check your .env file.")
|
| 25 |
st.stop()
|
| 26 |
|
| 27 |
+
# Hugging Face API URL
|
| 28 |
API_URL = "https://api-inference.huggingface.co/models/gpt2" # Replace with your desired model's API URL
|
| 29 |
headers = {
|
| 30 |
"Authorization": f"Bearer {HuggingFace_API_KEY}" # Replace with your actual API key
|
| 31 |
}
|
| 32 |
|
| 33 |
+
# Function to extract text from the uploaded PDF
|
|
|
|
| 34 |
def extract_text_from_pdf(pdf_file):
|
| 35 |
# Read the uploaded file as a byte stream
|
| 36 |
pdf_bytes = pdf_file.read()
|
|
|
|
| 42 |
text += page.get_text()
|
| 43 |
return text
|
| 44 |
|
| 45 |
+
# Function to extract relevant information from the CV using Hugging Face or Azure OpenAI
|
|
|
|
|
|
|
| 46 |
def extract_info_from_openai(text):
|
| 47 |
prompt = f"""
|
| 48 |
Extract the following information from this CV text:
|
|
|
|
| 56 |
{text}
|
| 57 |
"""
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
data = {"inputs": prompt}
|
| 60 |
response = requests.post(API_URL, headers=headers, json=data)
|
| 61 |
+
|
| 62 |
+
# If the Hugging Face response is successful, extract the generated text
|
| 63 |
+
if response.status_code == 200:
|
| 64 |
+
result = response.json() # Parse the JSON response
|
| 65 |
+
return result.get("generated_text", "Error: Unable to extract text.")
|
| 66 |
+
else:
|
| 67 |
+
return f"Error: {response.status_code} - {response.text}"
|
| 68 |
+
|
| 69 |
+
# Streamlit App UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
st.title("AI Screening")
|
|
|
|
| 71 |
st.write("Upload a CV PDF file, and the app will extract relevant information such as job title, location, skills, experience, and education.")
|
| 72 |
|
| 73 |
# File uploader
|
| 74 |
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
|
| 75 |
|
| 76 |
if uploaded_file is not None:
|
| 77 |
+
# Extract text from the uploaded PDF
|
| 78 |
text = extract_text_from_pdf(uploaded_file)
|
| 79 |
|
| 80 |
# Display the extracted text (optional)
|
| 81 |
st.subheader("Extracted Text from CV")
|
| 82 |
st.text_area("Text from CV", text, height=300)
|
| 83 |
|
| 84 |
+
# Extract relevant information using Hugging Face (or Azure OpenAI if you need)
|
| 85 |
extracted_info = extract_info_from_openai(text)
|
| 86 |
|
| 87 |
# Display the extracted information
|
| 88 |
st.subheader("Extracted Information")
|
| 89 |
+
st.write(extracted_info)
|