Vizznu19 commited on
Commit
303a7be
Β·
verified Β·
1 Parent(s): 8cce4a3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -13
app.py CHANGED
@@ -1,7 +1,7 @@
1
- # --- Imports ---
 
2
  import os
3
  import gradio as gr
4
- from getpass import getpass
5
  from llama_index.readers.file import PDFReader
6
  from llama_index.core import VectorStoreIndex
7
  from llama_index.llms.openai import OpenAI
@@ -9,15 +9,18 @@ from llama_index.core.memory import ChatMemoryBuffer
9
  from llama_index.core.chat_engine import ContextChatEngine
10
  from llama_index.core.llms import ChatMessage, MessageRole
11
 
12
- api_key = os.getenv("OPENAI_API_KEY")
13
- if not api_key:
14
- raise ValueError("OPENAI_API_KEY environment variable is required.")
 
 
 
15
 
16
  # --- Global Chat Engine ---
17
  chat_engine = None
18
- resume_text = "" # to extract follow-up context
19
 
20
- # --- Helper: Generate Follow-ups ---
21
  def generate_followups(user_question, resume_excerpt):
22
  llm = OpenAI(model="gpt-3.5-turbo")
23
  prompt = (
@@ -29,16 +32,16 @@ def generate_followups(user_question, resume_excerpt):
29
  try:
30
  response = llm.complete(prompt)
31
  return response.text.strip()
32
- except Exception as e:
33
  return "πŸ” Follow-up Suggestions:\n- Unable to generate follow-ups at this time."
34
 
35
- # --- Upload + Index Resume ---
36
  def upload_and_index(file):
37
  global chat_engine, resume_text
38
  try:
39
  reader = PDFReader()
40
  documents = reader.load_data(file=file.name)
41
- resume_text = "\n".join(doc.text for doc in documents[:1]) # small excerpt for followups
42
 
43
  index = VectorStoreIndex.from_documents(documents)
44
  retriever = index.as_retriever(similarity_top_k=3)
@@ -65,7 +68,7 @@ def upload_and_index(file):
65
  except Exception as e:
66
  return f"❌ Error during indexing: {str(e)}"
67
 
68
- # --- Chat Function (with follow-ups) ---
69
  def chat_with_bot(message, history):
70
  if chat_engine is None:
71
  return "❌ Please upload and index your resume first."
@@ -76,7 +79,7 @@ def chat_with_bot(message, history):
76
  except Exception as e:
77
  return f"❌ Error: {str(e)}"
78
 
79
- # --- Gradio UI ---
80
  with gr.Blocks() as iface:
81
  gr.Markdown("""
82
  <h1 style='text-align: center;'>Revue</h1>
@@ -99,4 +102,4 @@ with gr.Blocks() as iface:
99
  textbox=gr.Textbox(placeholder="e.g. What are my key strengths for data science?", lines=1),
100
  )
101
 
102
- iface.launch(share=True)
 
1
+ # app.py
2
+
3
  import os
4
  import gradio as gr
 
5
  from llama_index.readers.file import PDFReader
6
  from llama_index.core import VectorStoreIndex
7
  from llama_index.llms.openai import OpenAI
 
9
  from llama_index.core.chat_engine import ContextChatEngine
10
  from llama_index.core.llms import ChatMessage, MessageRole
11
 
12
+ # --- Use HF secret for OpenAI API key ---
13
+ OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
14
+ if not OPENAI_API_KEY:
15
+ raise EnvironmentError("OPENAI_API_KEY is not set in environment variables.")
16
+
17
+ os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
18
 
19
  # --- Global Chat Engine ---
20
  chat_engine = None
21
+ resume_text = ""
22
 
23
+ # --- Follow-up Generator ---
24
  def generate_followups(user_question, resume_excerpt):
25
  llm = OpenAI(model="gpt-3.5-turbo")
26
  prompt = (
 
32
  try:
33
  response = llm.complete(prompt)
34
  return response.text.strip()
35
+ except Exception:
36
  return "πŸ” Follow-up Suggestions:\n- Unable to generate follow-ups at this time."
37
 
38
+ # --- Upload & Index Resume ---
39
  def upload_and_index(file):
40
  global chat_engine, resume_text
41
  try:
42
  reader = PDFReader()
43
  documents = reader.load_data(file=file.name)
44
+ resume_text = "\n".join(doc.text for doc in documents[:1])
45
 
46
  index = VectorStoreIndex.from_documents(documents)
47
  retriever = index.as_retriever(similarity_top_k=3)
 
68
  except Exception as e:
69
  return f"❌ Error during indexing: {str(e)}"
70
 
71
+ # --- Chat Function ---
72
  def chat_with_bot(message, history):
73
  if chat_engine is None:
74
  return "❌ Please upload and index your resume first."
 
79
  except Exception as e:
80
  return f"❌ Error: {str(e)}"
81
 
82
+ # --- UI ---
83
  with gr.Blocks() as iface:
84
  gr.Markdown("""
85
  <h1 style='text-align: center;'>Revue</h1>
 
102
  textbox=gr.Textbox(placeholder="e.g. What are my key strengths for data science?", lines=1),
103
  )
104
 
105
+ iface.launch()