Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from llama_index.readers.file import PDFReader
|
| 4 |
+
from llama_index.core import VectorStoreIndex
|
| 5 |
+
from llama_index.core.chat_engine.types import BaseChatEngine
|
| 6 |
+
|
| 7 |
+
# Set your OpenAI API key here (or via Hugging Face Secrets)
|
| 8 |
+
os.environ['OPENAI_API_KEY'] = os.getenv("OPENAI_API_KEY")
|
| 9 |
+
|
| 10 |
+
# Globals
|
| 11 |
+
chat_engine: BaseChatEngine = None
|
| 12 |
+
|
| 13 |
+
# Function to process uploaded resume
|
| 14 |
+
def process_resume(file):
|
| 15 |
+
global chat_engine
|
| 16 |
+
reader = PDFReader()
|
| 17 |
+
documents = reader.load_data(file=file.name)
|
| 18 |
+
index = VectorStoreIndex.from_documents(documents)
|
| 19 |
+
chat_engine = index.as_chat_engine(chat_mode="condense_question", verbose=False)
|
| 20 |
+
return "✅ Resume uploaded and indexed! You can now ask questions."
|
| 21 |
+
|
| 22 |
+
# Chat function
|
| 23 |
+
def chat_with_resume(message, chat_history):
|
| 24 |
+
global chat_engine
|
| 25 |
+
if not chat_engine:
|
| 26 |
+
return "⚠️ Please upload a resume first.", chat_history
|
| 27 |
+
response = chat_engine.chat(message)
|
| 28 |
+
chat_history.append((message, response.response))
|
| 29 |
+
return "", chat_history
|
| 30 |
+
|
| 31 |
+
# Gradio UI
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
gr.Markdown("# 📄 Resume Chatbot\nUpload your resume and ask questions about your experience, skills, and more.")
|
| 34 |
+
|
| 35 |
+
with gr.Row():
|
| 36 |
+
file_input = gr.File(label="Upload Resume (PDF)", file_types=[".pdf"])
|
| 37 |
+
upload_button = gr.Button("Process Resume")
|
| 38 |
+
|
| 39 |
+
upload_output = gr.Textbox(label="Status")
|
| 40 |
+
|
| 41 |
+
upload_button.click(fn=process_resume, inputs=file_input, outputs=upload_output)
|
| 42 |
+
|
| 43 |
+
chatbot = gr.Chatbot(label="Chat with Resume")
|
| 44 |
+
message = gr.Textbox(placeholder="Ask something like: What are my key skills?", label="Your Question")
|
| 45 |
+
send = gr.Button("Send")
|
| 46 |
+
|
| 47 |
+
send.click(chat_with_resume, inputs=[message, chatbot], outputs=[message, chatbot])
|
| 48 |
+
|
| 49 |
+
demo.launch()
|