Spaces:
Sleeping
Sleeping
| import os | |
| os.environ['OPENAI_API_KEY'] = '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' # Replace with your key | |
| from llama_index.readers.file import PDFReader | |
| from llama_index.core import VectorStoreIndex | |
| from llama_index.llms.openai import OpenAI | |
| from llama_index.core.chat_engine import CondenseQuestionChatEngine | |
| chat_engine = None | |
| def load_resume(pdf_file): | |
| global chat_engine | |
| reader = PDFReader() | |
| documents = reader.load_data(file=pdf_file.name) | |
| index = VectorStoreIndex.from_documents(documents) | |
| llm = OpenAI(model="gpt-3.5-turbo") # Uses your OpenAI API key already set | |
| chat_engine = index.as_chat_engine( | |
| chat_mode="context", | |
| llm=llm, | |
| system_prompt=( | |
| "You are the person whose resume has been uploaded. " | |
| "Answer all questions in first person, confidently and professionally." | |
| "Reply in Markdown" | |
| ) | |
| ) | |
| return "β Resume uploaded and indexed. Ask away!" | |
| def ask_bot(user_input): | |
| if chat_engine is None: | |
| return "β Please upload a resume first." | |
| response = chat_engine.chat(user_input) | |
| return f"**Answer:**\n\n{response.response.strip()}" | |
| import gradio as gr | |
| chat_history = [] # to store (question, answer) tuples | |
| def ask_and_update(user_input): | |
| global chat_history | |
| if chat_engine is None: | |
| return "β Please upload a resume first.", "" | |
| # Get bot response | |
| response = chat_engine.chat(user_input) | |
| answer = response.response.strip() | |
| # Append Q&A to history | |
| chat_history.append((user_input, answer)) | |
| # Build Markdown display | |
| formatted_chat = "" | |
| for q, a in chat_history: | |
| formatted_chat += f"**You:** {q}\n\n**Bot:** {a}\n\n---\n" | |
| return "", formatted_chat # clear input, update chat display | |
| with gr.Blocks(theme=gr.themes.Base(), css=""" | |
| #title {text-align: center;} | |
| #ask_input textarea {font-size: 16px;} | |
| """) as demo: | |
| gr.Markdown("## π§ <span id='title'>Resume Q&A Assistant</span>") | |
| with gr.Group(): | |
| gr.Markdown("### π Upload Your Resume") | |
| with gr.Row(): | |
| resume_input = gr.File(label="Resume (PDF only)", file_types=[".pdf"]) | |
| upload_button = gr.Button("π€ Upload & Start") | |
| upload_output = gr.Textbox(label="Status", interactive=False) | |
| with gr.Group(): | |
| gr.Markdown("### π¬ Ask About Your Resume") | |
| chat_output = gr.Markdown("π Ask me anything about your resume!", elem_id="chat_log") | |
| chat_input = gr.Textbox( | |
| label="Ask a Question", | |
| placeholder="e.g., What are my strengths?", | |
| lines=2, | |
| elem_id="ask_input" | |
| ) | |
| upload_button.click(fn=load_resume, inputs=[resume_input], outputs=[upload_output]) | |
| chat_input.submit(fn=ask_and_update, inputs=[chat_input], outputs=[chat_input, chat_output]) | |
| demo.launch() | |