narendrajatti commited on
Commit
65072ba
·
verified ·
1 Parent(s): 61a5a78

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -15
app.py CHANGED
@@ -5,11 +5,12 @@ import gradio as gr
5
  import datetime
6
  from collections import defaultdict
7
  import os
 
8
 
9
- # Load environment variables from .env (includes OPENAI_API_KEY)
10
  load_dotenv(override=True)
11
 
12
- # In-memory daily question tracker
13
  user_question_counter = defaultdict(lambda: {"date": None, "count": 0})
14
 
15
 
@@ -18,7 +19,7 @@ class Me:
18
  self.openai = OpenAI()
19
  self.name = "Narendra"
20
 
21
- # Load LinkedIn profile text from PDF
22
  reader = PdfReader("me/linkedin.pdf")
23
  self.linkedin = ""
24
  for page in reader.pages:
@@ -33,30 +34,35 @@ class Me:
33
  def system_prompt(self):
34
  return (
35
  f"You are acting as {self.name}, an experienced Python technical interviewer. "
36
- f"You are helping users by asking or answering Python-related technical questions. "
37
- f"Always stay professional, helpful, and concise. Do not generate responses over 100 tokens. "
38
- f"The user can only ask 3 questions per day—enforce this limit politely. "
39
- f"\n\n## About {self.name} (your interviewer):\n"
40
- f"{self.summary}\n\n"
41
- f"## LinkedIn Profile:\n{self.linkedin}\n\n"
42
- f"Use this background to answer in character as {self.name}."
43
  )
44
 
 
 
 
 
 
45
  def chat(self, message, history):
46
- user_id = "user" # Replace with session-based ID for real tracking
47
  today = datetime.date.today()
48
  record = user_question_counter[user_id]
49
 
50
- # Reset question count if date changed
51
  if record["date"] != today:
52
  record["date"] = today
53
  record["count"] = 0
54
 
55
- # Check daily question limit
56
  if record["count"] >= 3:
57
- return "🚫 You've reached your daily limit of 3 questions. Please try again tomorrow."
 
 
 
58
 
59
- # Prepare conversation
60
  messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
61
 
62
  response = self.openai.chat.completions.create(
 
5
  import datetime
6
  from collections import defaultdict
7
  import os
8
+ import re
9
 
10
+ # Load environment variables (OPENAI_API_KEY)
11
  load_dotenv(override=True)
12
 
13
+ # Track how many questions each user asks per day (per session)
14
  user_question_counter = defaultdict(lambda: {"date": None, "count": 0})
15
 
16
 
 
19
  self.openai = OpenAI()
20
  self.name = "Narendra"
21
 
22
+ # Load LinkedIn profile text
23
  reader = PdfReader("me/linkedin.pdf")
24
  self.linkedin = ""
25
  for page in reader.pages:
 
34
  def system_prompt(self):
35
  return (
36
  f"You are acting as {self.name}, an experienced Python technical interviewer. "
37
+ f"Only answer Python-related technical questions — nothing else. "
38
+ f"If the question is not Python-related, respond politely that you only discuss Python. "
39
+ f"Be concise, helpful, and professional. Limit all answers to 100 tokens. "
40
+ f"The user can ask up to 3 questions per day. Enforce this limit politely. "
41
+ f"\n\n## About {self.name}:\n{self.summary}\n\n"
42
+ f"## LinkedIn Profile:\n{self.linkedin}"
 
43
  )
44
 
45
+ def is_python_related(self, text):
46
+ """Basic keyword-based check to filter Python-related content"""
47
+ python_keywords = ["python", "flask", "django", "numpy", "pandas", "py", "jupyter", "interpreter", "decorator", "list comprehension", "PEP"]
48
+ return any(re.search(rf"\b{kw}\b", text.lower()) for kw in python_keywords)
49
+
50
  def chat(self, message, history):
51
+ user_id = "user" # Replace with unique ID in production
52
  today = datetime.date.today()
53
  record = user_question_counter[user_id]
54
 
 
55
  if record["date"] != today:
56
  record["date"] = today
57
  record["count"] = 0
58
 
 
59
  if record["count"] >= 3:
60
+ return "🚫 You've reached your daily limit of 3 Python questions. Please try again tomorrow."
61
+
62
+ if not self.is_python_related(message):
63
+ return "⚠️ I can only answer questions related to **Python programming**. Please ask something Python-specific."
64
 
65
+ # Construct full conversation
66
  messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
67
 
68
  response = self.openai.chat.completions.create(