Spaces:
Build error
Build error
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +112 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,114 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import fitz # PyMuPDF
|
| 3 |
+
from langchain_openai import ChatOpenAI
|
| 4 |
+
from azure.core.credentials import AzureKeyCredential
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
import io
|
| 7 |
+
import os
|
| 8 |
+
import openai
|
| 9 |
+
import logging
|
| 10 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
| 11 |
+
from azure.identity import ManagedIdentityCredential # For Managed Identity
|
| 12 |
+
from azure.core.credentials import AzureKeyCredential
|
| 13 |
+
import requests
|
| 14 |
+
#openAI
|
| 15 |
+
# from langchain_openai import AzureChatOpenAI
|
| 16 |
|
| 17 |
+
|
| 18 |
+
# Load environment variables from .env file
|
| 19 |
+
load_dotenv()
|
| 20 |
+
|
| 21 |
+
#set env
|
| 22 |
+
ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
|
| 23 |
+
API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
|
| 24 |
+
DEPLOYMENT_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME")
|
| 25 |
+
azure_openai_embedding_model = os.getenv("AZURE_OPENAI_EMBEDDING_MODEL")
|
| 26 |
+
HuggingFace_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
|
| 27 |
+
HuggingFace_API_URL = os.getenv("HUGGINGFACE_API_URL")
|
| 28 |
+
|
| 29 |
+
# Check if the necessary environment variables are loaded
|
| 30 |
+
if not API_KEY or not ENDPOINT or not DEPLOYMENT_NAME:
|
| 31 |
+
st.error("Azure OpenAI credentials are missing. Please check your .env file.")
|
| 32 |
+
st.stop()
|
| 33 |
+
|
| 34 |
+
# Initialize the OpenAI client
|
| 35 |
+
#client = OpenAIClient(endpoint=ENDPOINT, credential=AzureKeyCredential(API_KEY))
|
| 36 |
+
|
| 37 |
+
#myCode
|
| 38 |
+
headers = {
|
| 39 |
+
"Authorization": f"Bearer {HuggingFace_API_KEY}" # Replace with your actual API key
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
# Function to extract text from the PDF
|
| 44 |
+
def extract_text_from_pdf(pdf_file):
|
| 45 |
+
# Read the uploaded file as a byte stream
|
| 46 |
+
pdf_bytes = pdf_file.read()
|
| 47 |
+
|
| 48 |
+
# Open the PDF from the byte stream
|
| 49 |
+
doc = fitz.open("pdf", pdf_bytes) # Fix: use the correct format to open the byte stream
|
| 50 |
+
text = ""
|
| 51 |
+
for page in doc:
|
| 52 |
+
text += page.get_text()
|
| 53 |
+
return text
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
# Function to extract relevant information from the CV using Azure OpenAI (ChatGPT)
|
| 58 |
+
def extract_info_from_openai(text):
|
| 59 |
+
prompt = f"""
|
| 60 |
+
Extract the following information from this CV text:
|
| 61 |
+
1. Job title
|
| 62 |
+
2. Location
|
| 63 |
+
3. Skills
|
| 64 |
+
4. Years of experience
|
| 65 |
+
5. Education level
|
| 66 |
+
|
| 67 |
+
Text:
|
| 68 |
+
{text}
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
system_message = SystemMessage(content=prompt)
|
| 72 |
+
messages = [system_message]
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
data = {"inputs": "Hello, Hugging Face!"}
|
| 76 |
+
#data = {"inputs": prompt}
|
| 77 |
+
response = requests.post(API_URL, headers=headers, json=data)
|
| 78 |
+
# Call the invoke method to get the response
|
| 79 |
+
# response = client.invoke(messages)
|
| 80 |
+
|
| 81 |
+
# # Request to Azure OpenAI (GPT-4)
|
| 82 |
+
# response = client.completions.create(
|
| 83 |
+
# deployment_name=DEPLOYMENT_NAME,
|
| 84 |
+
# prompt=prompt,
|
| 85 |
+
# max_tokens=5000,
|
| 86 |
+
# temperature=0.7
|
| 87 |
+
# )
|
| 88 |
+
|
| 89 |
+
# Parse the AI response
|
| 90 |
+
result = response.text #.json() #response.result
|
| 91 |
+
return result #result.choices[0].text.strip()
|
| 92 |
+
|
| 93 |
+
# Streamlit App
|
| 94 |
+
st.title("AI Screening")
|
| 95 |
+
st.title("CV Information Extractor with Azure OpenAI (GPT-4)")
|
| 96 |
+
st.write("Upload a CV PDF file, and the app will extract relevant information such as job title, location, skills, experience, and education.")
|
| 97 |
+
|
| 98 |
+
# File uploader
|
| 99 |
+
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
|
| 100 |
+
|
| 101 |
+
if uploaded_file is not None:
|
| 102 |
+
# Extract text from PDF
|
| 103 |
+
text = extract_text_from_pdf(uploaded_file)
|
| 104 |
+
|
| 105 |
+
# Display the extracted text (optional)
|
| 106 |
+
st.subheader("Extracted Text from CV")
|
| 107 |
+
st.text_area("Text from CV", text, height=300)
|
| 108 |
+
|
| 109 |
+
# Extract relevant info using Azure OpenAI (GPT-4)
|
| 110 |
+
extracted_info = extract_info_from_openai(text)
|
| 111 |
+
|
| 112 |
+
# Display the extracted information
|
| 113 |
+
st.subheader("Extracted Information")
|
| 114 |
+
st.write(extracted_info)
|