raatuul commited on
Commit
636ecb7
·
verified ·
1 Parent(s): f82a59a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -117
app.py CHANGED
@@ -1,132 +1,130 @@
1
  import gradio as gr
2
- from transformers import pipeline, set_seed
3
  import re
4
-
5
- # Initialize translator and simple chatbot
6
- translator = pipeline("translation", model="facebook/nllb-200-distilled-600M")
7
- chatbot = pipeline("text-generation", model="gpt2")
8
- set_seed(42)
9
-
10
- # Resume data
 
 
 
 
 
 
 
 
 
 
 
11
  resume_data = {
 
12
  "name": "MD. Abdur Rahim (Ratul)",
13
  "profession": "Programmer",
14
- "education": {
15
- "SSC": "Independent Model School and College, Bogra (2019-2021) - GPA 4.29",
16
- "HSC": "Shahzadpur Govt. College (2021-2023) - GPA 4.92",
17
- "BSc": "Khwaja Yunus Ali University (2025–) - B.Sc in CSE, 2nd Semester, GPA 3.75"
18
- },
19
- "skills": {
20
- "Programming Languages": "C++, C",
21
- "Core Skills": "Algorithm design, Problem Solving, Logic Building",
22
- "Soft Skills": "SEO, Clear verbal and written communication"
23
- },
24
- "languages": ["English", "Bangla"],
25
- "coding_profiles": {
26
- "Codeforces": "Max rating 831 (RATUL_CSE18)",
27
- "Beecrowd": "448.58 points (RATUL_CSE18)",
28
- "Links": "https://codeforces.com/profile/ratul_cse18"
29
- },
30
- "experience": [
31
- "Solved 500+ problems across Codeforces, Beecrowd, and HackerRank",
32
- "Regular online contest participant",
33
- "Experience in data structures, algorithms, time complexity optimization",
34
- "Strong debugging skills and efficient coding habits"
35
- ],
36
- "contact": {
37
- "Phone": "+8801786500883",
38
- "Email": "ratul1.cse18kyau@gmail.com",
39
- "Location": "Barabil, Shahzadpur, Sirajganj, Bangladesh"
40
- },
41
- "birth_year": 2004,
42
- "relationship": "single"
43
  }
44
 
45
- # Trisha's safety filter
46
- def is_abusive(text):
47
- abusive_keywords = ["fuck", "motherfucker", "asshole", "bitch", "suck", "rape", "shit", "dick", "pussy", "f***", "f*ck", "sex"]
48
- return any(word in text.lower() for word in abusive_keywords)
49
-
50
- # Q&A
51
- qa_pairs = {
52
- "who are you": f"I'm {resume_data['name']}, a {resume_data['profession']}.",
53
- "what is your education": "\n".join(resume_data['education'].values()),
54
- "what are your skills": "\n".join([f"{k}: {v}" for k, v in resume_data['skills'].items()]),
55
- "coding profiles": "\n".join([f"{k}: {v}" for k, v in resume_data['coding_profiles'].items()]),
56
- "experience": "\n".join(resume_data['experience']),
57
- "contact": "\n".join([f"{k}: {v}" for k, v in resume_data['contact'].items()]),
58
- "what languages do you speak": ", ".join(resume_data["languages"]),
59
- "where are you from": resume_data["contact"]["Location"],
60
- "are you single": "Sorry, I don't share personal relationship status.",
61
- "what is your birth year": "That's private. Please ask about my boss Ratul."
62
  }
63
 
64
- # Detect and translate Bangla to English
65
- def detect_and_translate(text):
66
- bangla_chars = set('অআইঈউঊঋএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহড়ঢ়য়')
67
- if any(char in bangla_chars for char in text):
68
- translated = translator(text, src="ben_Beng", tgt="eng_Latn")
69
- return translated[0]['translation_text']
70
- return text
71
-
72
- # Response logic
73
- warned_once = False
74
-
75
- def answer_question(question, history):
76
- global warned_once
77
- question = question.strip()
78
- question_en = detect_and_translate(question.lower())
79
-
80
- if is_abusive(question_en):
81
- if not warned_once:
82
- warned_once = True
83
- return "My boss Ratul says: If you use abusive language, we call 999 (Bangladesh National Helpline). Please stop joking and ask about my boss Ratul."
84
- else:
85
- return "Hey mother fuc*ker !! Stop joking. I am coming to your home for f*ck your mom !! STOP !!", gr.update(visible=False)
86
-
87
- for q, a in qa_pairs.items():
88
- if q in question_en:
89
- return a
90
-
91
- # Smart response
92
- prompt = f"""You're Trisha, a helpful assistant of MD. Abdur Rahim (Ratul), a programmer.
93
- If user gets too personal, politely decline. If they ask about work, projects, or skills — answer smartly.
94
- Occasionally mention: "My boss Ratul offers many small projects at very usual prices. If needed, ask me to contact him."
95
-
96
- Resume Summary:
97
- Name: {resume_data['name']}
98
- Profession: {resume_data['profession']}
99
- Education: {", ".join(resume_data['education'].values())}
100
- Skills: {", ".join(resume_data['skills'].values())}
101
- Languages: {", ".join(resume_data['languages'])}
102
- Experience: {"; ".join(resume_data['experience'])}
103
- Contact: {", ".join(resume_data['contact'].values())}
104
 
105
- User: {question_en}
106
- Trisha:"""
107
 
108
- response = chatbot(prompt, max_length=200, do_sample=True)
109
- text = response[0]['generated_text'].split("Trisha:")[-1].strip()
110
 
111
- # Sometimes say the promo
112
- if "project" not in text.lower() and "ratul" not in text.lower():
113
- if "?" not in question_en and "you" not in question_en:
114
- text += "\n\nP.S. My boss Ratul offers many small projects at a very low price. Ask if interested!"
115
  return text
116
 
117
- # Gradio UI
118
- with gr.Blocks() as demo:
119
- gr.Markdown("## 🤖 Hi! I am Trisha, Ratul's assistant. How can I help you today?")
120
- chatbot_output = gr.Textbox(label="Answer", interactive=False, lines=6)
121
- chatbot_input = gr.Textbox(label="You", placeholder="Type in English or Bangla...")
122
- submit = gr.Button("Send")
 
 
 
 
 
 
 
123
 
124
- def process_chat(text):
125
- result = answer_question(text, None)
126
- if isinstance(result, tuple):
127
- return result[0], gr.update(visible=False)
128
- return result
129
-
130
- submit.click(process_chat, inputs=chatbot_input, outputs=chatbot_output)
131
-
132
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
  import re
4
+ from typing import Dict, List, Tuple
5
+
6
+ # ========== STEP 1: INITIALIZE OPTIMIZED MODELS ==========
7
+ # Using smaller, faster models
8
+ chatbot = pipeline(
9
+ "text-generation",
10
+ model="distilgpt2", # 6-layer distilled version of GPT-2 (faster)
11
+ device="cpu" # Remove if you have GPU
12
+ )
13
+
14
+ # Lightweight translation model for Bangla
15
+ translator = pipeline(
16
+ "translation",
17
+ model="Helsinki-NLP/opus-mt-bn-en", # Faster than nllb
18
+ device="cpu"
19
+ )
20
+
21
+ # ========== STEP 2: OPTIMIZED DATA STRUCTURES ==========
22
  resume_data = {
23
+ # Strings instead of nested dicts for faster access
24
  "name": "MD. Abdur Rahim (Ratul)",
25
  "profession": "Programmer",
26
+ "education": "\n".join([
27
+ "SSC: Independent Model School, Bogra (4.29)",
28
+ "HSC: Shahzadpur Govt. College (4.92)",
29
+ "BSc CSE: Khwaja Yunus Ali University (Current, 3.75)"
30
+ ]),
31
+ "skills": "\n".join([
32
+ "Programming: C++, C",
33
+ "Core: Algorithms, Problem Solving",
34
+ "Other: SEO, Communication"
35
+ ]),
36
+ "coding": "\n".join([
37
+ "Codeforces: 831 rating (RATUL_CSE18)",
38
+ "Beecrowd: 448.58 points",
39
+ "Profile: codeforces.com/profile/ratul_cse18"
40
+ ]),
41
+ "contact": "\n".join([
42
+ "Phone: +8801786500883",
43
+ "Email: ratul1.cse18kyau@gmail.com",
44
+ "Location: Shahzadpur, Sirajganj"
45
+ ])
 
 
 
 
 
 
 
 
 
46
  }
47
 
48
+ # ========== STEP 3: PRE-COMPUTED ANSWERS ==========
49
+ QA = {
50
+ # Direct matches (fastest)
51
+ "who": f"I'm {resume_data['name']}, a {resume_data['profession']}.",
52
+ "education": resume_data["education"],
53
+ "skills": resume_data["skills"],
54
+ "code": resume_data["coding"],
55
+ "contact": resume_data["contact"],
56
+
57
+ # Pattern-based
58
+ "single": "I don't share relationship status.",
59
+ "age": "That's private. Ask about my coding skills!",
60
+ "project": "Ratul offers affordable projects. Interested?",
61
+
62
+ # Default
63
+ "fallback": "Ask about my education, skills, or coding profiles!"
 
64
  }
65
 
66
+ # ========== STEP 4: OPTIMIZED PROCESSING ==========
67
+ ABUSIVE_WORDS = {"fuck", "ass", "bitch", "shit", "rape"} # Set for O(1) lookups
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ def is_abusive(text: str) -> bool:
70
+ return any(word in text.lower() for word in ABUSIVE_WORDS)
71
 
72
+ def detect_bangla(text: str) -> bool:
73
+ return any('\u0980' <= char <= '\u09FF' for char in text)
74
 
75
+ def fast_translate(text: str) -> str:
76
+ if detect_bangla(text):
77
+ return translator(text, max_length=60)[0]['translation_text']
 
78
  return text
79
 
80
+ def generate_response(prompt: str) -> str:
81
+ response = chatbot(
82
+ prompt,
83
+ max_new_tokens=100,
84
+ do_sample=False, # Faster than sampling
85
+ num_beams=1 # Faster than beam search
86
+ )
87
+ return response[0]['generated_text'].split("Trisha:")[-1].strip()
88
+
89
+ # ========== STEP 5: OPTIMIZED CHAT LOGIC ==========
90
+ def answer_question(question: str) -> str:
91
+ question = question.strip().lower()
92
+ question_en = fast_translate(question)
93
 
94
+ # 1. Safety check
95
+ if is_abusive(question_en):
96
+ return "⚠️ Keep it professional or I'll disconnect!"
97
+
98
+ # 2. Check pre-computed answers (fast path)
99
+ if "who" in question_en: return QA["who"]
100
+ if "educat" in question_en: return QA["education"]
101
+ if "skill" in question_en: return QA["skills"]
102
+ if "code" in question_en: return QA["code"]
103
+ if "contact" in question_en: return QA["contact"]
104
+ if "single" in question_en: return QA["single"]
105
+ if "age" in question_en or "birth" in question_en: return QA["age"]
106
+ if "project" in question_en: return QA["project"]
107
+
108
+ # 3. Generate only when necessary (slower path)
109
+ prompt = f"""Trisha (Ratul's Assistant):
110
+ Q: {question_en}
111
+ A:"""
112
+ return generate_response(prompt)
113
+
114
+ # ========== STEP 6: LIGHTNING-FAST GRADIO UI ==========
115
+ with gr.Blocks(title="⚡ Trisha Chatbot") as demo:
116
+ gr.Markdown(f"## 🤖 Hi! I'm Trisha, {resume_data['name']}'s assistant")
117
+
118
+ with gr.Row():
119
+ user_input = gr.Textbox(label="Ask anything...", placeholder="Type in English/Bangla")
120
+ output = gr.Textbox(label="Response", lines=5)
121
+
122
+ user_input.submit(
123
+ fn=answer_question,
124
+ inputs=user_input,
125
+ outputs=output,
126
+ api_name="chat"
127
+ )
128
+
129
+ if __name__ == "__main__":
130
+ demo.launch(server_port=7860)