Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,10 +7,10 @@ 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
|
14 |
user_question_counter = defaultdict(lambda: {"date": None, "count": 0})
|
15 |
|
16 |
|
@@ -19,7 +19,7 @@ class Me:
|
|
19 |
self.openai = OpenAI()
|
20 |
self.name = "Narendra"
|
21 |
|
22 |
-
# Load LinkedIn
|
23 |
reader = PdfReader("me/linkedin.pdf")
|
24 |
self.linkedin = ""
|
25 |
for page in reader.pages:
|
@@ -27,31 +27,36 @@ class Me:
|
|
27 |
if text:
|
28 |
self.linkedin += text
|
29 |
|
30 |
-
# Load summary
|
31 |
with open("me/summary.txt", "r", encoding="utf-8") as f:
|
32 |
self.summary = f.read()
|
33 |
|
34 |
def system_prompt(self):
|
35 |
return (
|
36 |
f"You are acting as {self.name}, an experienced Python technical interviewer. "
|
37 |
-
f"Only answer
|
38 |
-
f"
|
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 |
-
"""
|
47 |
-
python_keywords = [
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
today = datetime.date.today()
|
53 |
-
record = user_question_counter[
|
54 |
|
|
|
55 |
if record["date"] != today:
|
56 |
record["date"] = today
|
57 |
record["count"] = 0
|
@@ -60,9 +65,9 @@ class Me:
|
|
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
|
64 |
|
65 |
-
#
|
66 |
messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
|
67 |
|
68 |
response = self.openai.chat.completions.create(
|
@@ -77,4 +82,5 @@ class Me:
|
|
77 |
|
78 |
if __name__ == "__main__":
|
79 |
me = Me()
|
80 |
-
|
|
|
|
7 |
import os
|
8 |
import re
|
9 |
|
10 |
+
# Load environment variables (includes OPENAI_API_KEY)
|
11 |
load_dotenv(override=True)
|
12 |
|
13 |
+
# Track daily question count per user IP
|
14 |
user_question_counter = defaultdict(lambda: {"date": None, "count": 0})
|
15 |
|
16 |
|
|
|
19 |
self.openai = OpenAI()
|
20 |
self.name = "Narendra"
|
21 |
|
22 |
+
# Load LinkedIn text
|
23 |
reader = PdfReader("me/linkedin.pdf")
|
24 |
self.linkedin = ""
|
25 |
for page in reader.pages:
|
|
|
27 |
if text:
|
28 |
self.linkedin += text
|
29 |
|
30 |
+
# Load professional summary
|
31 |
with open("me/summary.txt", "r", encoding="utf-8") as f:
|
32 |
self.summary = f.read()
|
33 |
|
34 |
def system_prompt(self):
|
35 |
return (
|
36 |
f"You are acting as {self.name}, an experienced Python technical interviewer. "
|
37 |
+
f"Only answer questions related to Python programming — skip all non-Python topics. "
|
38 |
+
f"Do not exceed 100 tokens in your response. "
|
|
|
|
|
39 |
f"\n\n## About {self.name}:\n{self.summary}\n\n"
|
40 |
f"## LinkedIn Profile:\n{self.linkedin}"
|
41 |
)
|
42 |
|
43 |
def is_python_related(self, text):
|
44 |
+
"""Smarter keyword-based filter for Python-related questions"""
|
45 |
+
python_keywords = [
|
46 |
+
"python", "list", "dictionary", "dict", "tuple", "set", "loop",
|
47 |
+
"for", "while", "comprehension", "function", "class", "exception",
|
48 |
+
"PEP", "decorator", "lambda", "flask", "django", "pandas", "numpy",
|
49 |
+
"jupyter", "interpreter", "import", "package", "virtualenv", "pytest"
|
50 |
+
]
|
51 |
+
text_lower = text.lower()
|
52 |
+
return any(kw in text_lower for kw in python_keywords)
|
53 |
+
|
54 |
+
def chat(self, message, history, request: gr.Request):
|
55 |
+
ip = request.client.host or "unknown"
|
56 |
today = datetime.date.today()
|
57 |
+
record = user_question_counter[ip]
|
58 |
|
59 |
+
# Reset daily count if new day
|
60 |
if record["date"] != today:
|
61 |
record["date"] = today
|
62 |
record["count"] = 0
|
|
|
65 |
return "🚫 You've reached your daily limit of 3 Python questions. Please try again tomorrow."
|
66 |
|
67 |
if not self.is_python_related(message):
|
68 |
+
return "⚠️ I can only answer questions related to Python programming. Please ask something Python-specific."
|
69 |
|
70 |
+
# Build messages for OpenAI
|
71 |
messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
|
72 |
|
73 |
response = self.openai.chat.completions.create(
|
|
|
82 |
|
83 |
if __name__ == "__main__":
|
84 |
me = Me()
|
85 |
+
# Pass `request` to get user IP
|
86 |
+
gr.ChatInterface(me.chat, type="messages", additional_inputs=[], concurrency_limit=None).launch(share=True)
|