File size: 2,391 Bytes
dbe3e79 | 1 2 3 4 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | # π Securely Set OpenAI API Key
import os
import getpass
os.environ["OPENAI_API_KEY"] = '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' # <- Replace with your key
# π Imports
from llama_index.core import VectorStoreIndex, download_loader
import gradio as gr
# π Load PDFReader dynamically (works across versions)
PDFReader = download_loader("PDFReader")
# π Global variables to store index and query engine
index = None
query_engine = None
# π₯ Load and Process Resume PDF
def process_resume(file):
global index, query_engine
try:
file_path = file.name
loader = PDFReader()
documents = loader.load_data(file_path)
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
return "β
Resume uploaded and indexed successfully."
except Exception as e:
return f"β Error processing resume: {str(e)}"
# β Answer Questions About Resume
def query_resume(question):
global query_engine
if not query_engine:
return "β Please upload a resume first."
try:
response = query_engine.query(question)
return str(response)
except Exception as e:
return f"β Error during query: {str(e)}"
# π€ Gradio Interface for Resume Upload
upload_interface = gr.Interface(
fn=process_resume,
inputs=gr.File(label="Upload Resume (PDF)", file_types=[".pdf"]),
outputs="text",
title="π Resume Uploader",
description="Upload your PDF resume to enable question-answering."
)
# π¬ Gradio Interface for Resume Querying
query_interface = gr.Interface(
fn=query_resume,
inputs=gr.Textbox(lines=2, placeholder="Ask something like: What are my skills?"),
outputs="text",
title="π€ Resume Query Bot",
description="Ask questions about your uploaded resume.",
examples=[
["What is my email?"],
["What are my technical skills?"],
["List the projects I have done."],
["Where did I study?"],
["Do I have any certifications?"]
]
)
# π§© Combine Both Interfaces
app = gr.TabbedInterface(
interface_list=[upload_interface, query_interface],
tab_names=["Upload Resume", "Ask Questions"]
)
# π Launch the App
app.launch(share=True) |