AI-Screening / src /streamlit_app.py
mdakhras's picture
Update src/streamlit_app.py
41e3892 verified
raw
history blame
3.49 kB
import streamlit as st
import fitz # PyMuPDF
from langchain_openai import ChatOpenAI
from azure.core.credentials import AzureKeyCredential
from dotenv import load_dotenv
import io
import os
import openai
import logging
from langchain_core.messages import HumanMessage, SystemMessage
from azure.identity import ManagedIdentityCredential # For Managed Identity
from azure.core.credentials import AzureKeyCredential
import requests
#openAI
# from langchain_openai import AzureChatOpenAI
# Load environment variables from .env file
load_dotenv()
#set env
ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
DEPLOYMENT_NAME = os.getenv("AZURE_OPENAI_DEPLOYMENT_NAME")
azure_openai_embedding_model = os.getenv("AZURE_OPENAI_EMBEDDING_MODEL")
HuggingFace_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
HuggingFace_API_URL = os.getenv("HUGGINGFACE_API_URL")
# Check if the necessary environment variables are loaded
if not API_KEY or not ENDPOINT or not DEPLOYMENT_NAME:
st.error("Azure OpenAI credentials are missing. Please check your .env file.")
st.stop()
# Initialize the OpenAI client
#client = OpenAIClient(endpoint=ENDPOINT, credential=AzureKeyCredential(API_KEY))
#myCode
headers = {
"Authorization": f"Bearer {HuggingFace_API_KEY}" # Replace with your actual API key
}
# Function to extract text from the PDF
def extract_text_from_pdf(pdf_file):
# Read the uploaded file as a byte stream
pdf_bytes = pdf_file.read()
# Open the PDF from the byte stream
doc = fitz.open("pdf", pdf_bytes) # Fix: use the correct format to open the byte stream
text = ""
for page in doc:
text += page.get_text()
return text
# Function to extract relevant information from the CV using Azure OpenAI (ChatGPT)
def extract_info_from_openai(text):
prompt = f"""
Extract the following information from this CV text:
1. Job title
2. Location
3. Skills
4. Years of experience
5. Education level
Text:
{text}
"""
system_message = SystemMessage(content=prompt)
messages = [system_message]
data = {"inputs": "Hello, Hugging Face!"}
#data = {"inputs": prompt}
response = requests.post(API_URL, headers=headers, json=data)
# Call the invoke method to get the response
# response = client.invoke(messages)
# # Request to Azure OpenAI (GPT-4)
# response = client.completions.create(
# deployment_name=DEPLOYMENT_NAME,
# prompt=prompt,
# max_tokens=5000,
# temperature=0.7
# )
# Parse the AI response
result = response.text #.json() #response.result
return result #result.choices[0].text.strip()
# Streamlit App
st.title("AI Screening")
st.title("CV Information Extractor with Azure OpenAI (GPT-4)")
st.write("Upload a CV PDF file, and the app will extract relevant information such as job title, location, skills, experience, and education.")
# File uploader
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
if uploaded_file is not None:
# Extract text from PDF
text = extract_text_from_pdf(uploaded_file)
# Display the extracted text (optional)
st.subheader("Extracted Text from CV")
st.text_area("Text from CV", text, height=300)
# Extract relevant info using Azure OpenAI (GPT-4)
extracted_info = extract_info_from_openai(text)
# Display the extracted information
st.subheader("Extracted Information")
st.write(extracted_info)