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 (
|
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,26 +27,26 @@ class Me:
|
|
27 |
if text:
|
28 |
self.linkedin += text
|
29 |
|
30 |
-
# Load
|
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
|
38 |
-
f"
|
39 |
-
f"
|
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)
|
@@ -56,7 +56,6 @@ class Me:
|
|
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
|
@@ -67,20 +66,23 @@ class Me:
|
|
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(
|
74 |
model="gpt-4o-mini",
|
75 |
messages=messages,
|
76 |
-
max_tokens=
|
77 |
)
|
78 |
|
79 |
record["count"] += 1
|
80 |
-
return
|
81 |
|
82 |
|
83 |
if __name__ == "__main__":
|
84 |
me = Me()
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
7 |
import os
|
8 |
import re
|
9 |
|
10 |
+
# Load environment variables (like OPENAI_API_KEY)
|
11 |
load_dotenv(override=True)
|
12 |
|
13 |
+
# Track questions per 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 profile
|
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 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. If the question is unrelated to Python, say so. "
|
38 |
+
f"All answers must be under 100 tokens.\n\n"
|
39 |
+
f"## 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 |
python_keywords = [
|
45 |
"python", "list", "dictionary", "dict", "tuple", "set", "loop",
|
46 |
"for", "while", "comprehension", "function", "class", "exception",
|
47 |
"PEP", "decorator", "lambda", "flask", "django", "pandas", "numpy",
|
48 |
+
"jupyter", "interpreter", "import", "package", "virtualenv", "pytest",
|
49 |
+
"recursion", "palindrome", "factorial", "generator", "iterator"
|
50 |
]
|
51 |
text_lower = text.lower()
|
52 |
return any(kw in text_lower for kw in python_keywords)
|
|
|
56 |
today = datetime.date.today()
|
57 |
record = user_question_counter[ip]
|
58 |
|
|
|
59 |
if record["date"] != today:
|
60 |
record["date"] = today
|
61 |
record["count"] = 0
|
|
|
66 |
if not self.is_python_related(message):
|
67 |
return "⚠️ I can only answer questions related to Python programming. Please ask something Python-specific."
|
68 |
|
|
|
69 |
messages = [{"role": "system", "content": self.system_prompt()}] + history + [{"role": "user", "content": message}]
|
|
|
70 |
response = self.openai.chat.completions.create(
|
71 |
model="gpt-4o-mini",
|
72 |
messages=messages,
|
73 |
+
max_tokens=50
|
74 |
)
|
75 |
|
76 |
record["count"] += 1
|
77 |
+
return response.choices[0].message.content
|
78 |
|
79 |
|
80 |
if __name__ == "__main__":
|
81 |
me = Me()
|
82 |
+
gr.ChatInterface(
|
83 |
+
fn=me.chat,
|
84 |
+
type="messages",
|
85 |
+
title="👋 Narendra is your Python Interviewer. Ask your Python questions (max 3/day).",
|
86 |
+
concurrency_limit=None,
|
87 |
+
theme="default"
|
88 |
+
).launch(share=True, show_api=False)
|