mdakhras commited on
Commit
41e3892
·
verified ·
1 Parent(s): 4266fb1

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
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)