AffandiGhazi commited on
Commit
823eded
Β·
verified Β·
1 Parent(s): 95b85ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -98
app.py CHANGED
@@ -14,131 +14,99 @@ def get_gemini_response(prompt):
14
  genai.configure(api_key=api_key)
15
  model = genai.GenerativeModel("gemini-2.0-flash")
16
  try:
17
- response = model.generate_content(prompt)
18
  return response.text
19
  except Exception as e:
20
  return f"❌ Gemini API Error: {str(e)}"
21
 
22
- # Remove or replace Unicode characters not supported by FPDF
23
  def sanitize_text(text):
24
- text = text.replace("–", "-").replace("’", "'").replace("β€œ", '"').replace("”", '"')
25
- text = re.sub(r"[^\x00-\x7F]+", "", text) # remove other non-ASCII characters
26
- return text
27
-
28
- # Custom PDF class with header and footer
 
 
 
 
 
 
 
 
 
 
 
 
29
  class PDF(FPDF):
30
  def header(self):
31
  self.set_font("Arial", "B", 12)
32
  self.set_text_color(40, 40, 40)
33
- self.multi_cell(0, 10, '"Success is where preparation and opportunity meet." - Bobby Unser', align='C')
34
  self.ln(5)
35
 
36
  def footer(self):
37
  self.set_y(-15)
38
  self.set_font("Arial", "I", 8)
39
  self.set_text_color(100, 100, 100)
40
- # Added your contact information here
41
  footer_text = f"Generated on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} | Created by Mahar Affandi Noor Ghazi - Pace GK Academy | Contact: +92 311 750 5369"
42
- self.cell(0, 10, footer_text, align='C')
43
 
44
  def create_pdf(interview_text, note_text, name):
45
  pdf = PDF()
46
  pdf.add_page()
47
- pdf.set_font("Times", "B", 16)
48
- pdf.cell(0, 10, f"Mock Interview: {name}", ln=True, align='C')
49
- pdf.ln(10)
50
-
 
 
 
 
 
 
 
 
 
51
  pdf.set_font("Times", "", 12)
 
52
  interview_text = sanitize_text(interview_text)
53
- lines = interview_text.split("\n")
54
- for line in lines:
55
- pdf.multi_cell(0, 8, line)
56
- pdf.ln(1)
57
-
58
- pdf.ln(10)
 
 
 
 
 
 
 
 
59
  pdf.set_font("Times", "B", 14)
60
- pdf.cell(0, 10, "Special Note & Recommendations", ln=True)
 
 
 
 
61
  pdf.set_font("Times", "", 12)
62
-
63
  note_text = sanitize_text(note_text)
64
- note_lines = note_text.split("\n")
65
- for line in note_lines:
66
- pdf.multi_cell(0, 8, line)
67
- pdf.ln(1)
68
-
69
- # Added your credentials at the end of the document
70
- pdf.ln(15)
71
- pdf.set_font("Times", "I", 10)
72
- pdf.set_text_color(0, 0, 128) # Navy blue color
73
- pdf.multi_cell(0, 6, "Prepared by:\nMahar Affandi Noor Ghazi\nPace GK Academy\nContact: +92 311 750 5369", align='C')
74
-
75
  filename = f"{name.replace(' ', '_')}_interview.pdf"
76
  pdf.output(filename)
77
  return filename
78
 
79
- # Rest of the Streamlit UI code remains the same...
80
- # (The Streamlit UI section below this point remains unchanged from your original code)
81
-
82
- # Streamlit UI
83
- st.set_page_config(page_title="Interview Generator", layout="centered")
84
- st.title("πŸ“š Mock Interview Generator for πŸ‡΅πŸ‡° Competitive Exams")
85
-
86
- with st.form("interview_form"):
87
- name = st.text_input("Full Name")
88
- father_name = st.text_input("Father's Name")
89
- district = st.text_input("District of Domicile")
90
- tehsil = st.text_input("Tehsil")
91
- bachelors = st.text_input("Bachelor's Degree (e.g., BSc Physics)")
92
- masters = st.text_input("Master's Degree (e.g., MSc Public Admin or 'None')")
93
- department_post = st.text_input("Department and Post (e.g., Education Dept - Lecturer English)")
94
- exam_type = st.selectbox("Exam Type", ["PPSC", "FPSC", "CSS", "PMS", "Other"])
95
- hobby = st.text_input("Hobby")
96
- fav_personality = st.text_input("Favorite Personality")
97
- submitted = st.form_submit_button("Generate Interview")
98
-
99
- if submitted:
100
- if all([name, father_name, district, tehsil, bachelors, masters, department_post, exam_type, hobby, fav_personality]):
101
- # Main interview prompt
102
- interview_prompt = f"""
103
- Generate a detailed mock interview based on the following Pakistani candidate's profile:
104
- - Name: {name}
105
- - Father's Name: {father_name}
106
- - District: {district}
107
- - Tehsil: {tehsil}
108
- - Bachelor's Degree: {bachelors}
109
- - Master's Degree: {masters}
110
- - Exam Type: {exam_type}
111
- - Department and Post: {department_post}
112
- - Hobby: {hobby}
113
- - Favorite Personality: {fav_personality}
114
- Include questions from:
115
- - Name-based personalities
116
- - District/Tehsil-specific geography, history, administration
117
- - Educational background
118
- - Post/Department-specific issues
119
- - Pakistan's geography, national & international affairs, sports
120
- - Government projects related to education
121
- - Situational judgment and problem-solving
122
- Format all in clear, numbered interview questions.
123
- """
124
-
125
- # Recommendation note prompt
126
- note_prompt = f"""
127
- Based on the profile of a candidate applying for the post of {department_post} through {exam_type},
128
- give a special note with motivational advice and suggest specific areas the candidate should study.
129
- Keep the tone encouraging and professional.
130
- """
131
-
132
- with st.spinner("Generating your personalized interview..."):
133
- interview_text = get_gemini_response(interview_prompt)
134
- note_text = get_gemini_response(note_prompt)
135
-
136
- st.success("βœ… Interview Generated Successfully!")
137
- st.text_area("πŸ“‹ Interview Questions", interview_text, height=400)
138
- st.text_area("πŸ“ Special Note", note_text, height=200)
139
 
140
- file_path = create_pdf(interview_text, note_text, name)
141
- with open(file_path, "rb") as f:
142
- st.download_button("πŸ“„ Download PDF with Recommendations", f, file_name=file_path, mime="application/pdf")
143
- else:
144
- st.warning("⚠️ Please complete all fields to proceed.")
 
14
  genai.configure(api_key=api_key)
15
  model = genai.GenerativeModel("gemini-2.0-flash")
16
  try:
17
+ response = model.generate_content(prompt + " Use plain text format without markdown, avoid symbols like **, #, or any special formatting.")
18
  return response.text
19
  except Exception as e:
20
  return f"❌ Gemini API Error: {str(e)}"
21
 
22
+ # Enhanced text sanitization
23
  def sanitize_text(text):
24
+ # Replace problematic Unicode characters
25
+ replacements = {
26
+ "–": "-", "’": "'", "β€œ": '"', "”": '"',
27
+ "β€’": "*", "●": "*", "ο‚·": "*", "β‹―": "..."
28
+ }
29
+ for k, v in replacements.items():
30
+ text = text.replace(k, v)
31
+
32
+ # Remove markdown and special formatting
33
+ text = re.sub(r'\*\*|\*|__|_|~~|`|#+', '', text) # Remove markdown symbols
34
+ text = re.sub(r'\[(.*?)\]\(.*?\)', r'\1', text) # Remove links but keep text
35
+ text = re.sub(r'<.*?>', '', text) # Remove HTML tags
36
+ text = re.sub(r'\n{3,}', '\n\n', text) # Reduce excessive newlines
37
+ text = re.sub(r'[^\x00-\x7F]+', ' ', text) # Replace non-ASCII with space
38
+ return text.strip()
39
+
40
+ # Improved PDF class with better formatting
41
  class PDF(FPDF):
42
  def header(self):
43
  self.set_font("Arial", "B", 12)
44
  self.set_text_color(40, 40, 40)
45
+ self.cell(0, 10, '"Success is where preparation and opportunity meet." - Bobby Unser', 0, 1, 'C')
46
  self.ln(5)
47
 
48
  def footer(self):
49
  self.set_y(-15)
50
  self.set_font("Arial", "I", 8)
51
  self.set_text_color(100, 100, 100)
 
52
  footer_text = f"Generated on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} | Created by Mahar Affandi Noor Ghazi - Pace GK Academy | Contact: +92 311 750 5369"
53
+ self.cell(0, 10, footer_text, 0, 0, 'C')
54
 
55
  def create_pdf(interview_text, note_text, name):
56
  pdf = PDF()
57
  pdf.add_page()
58
+
59
+ # Main title
60
+ pdf.set_font("Times", "B", 18)
61
+ pdf.cell(0, 10, f"Mock Interview: {name}", ln=1, align='C')
62
+ pdf.ln(12)
63
+
64
+ # Interview questions section
65
+ pdf.set_font("Times", "B", 14)
66
+ pdf.set_text_color(0, 0, 139) # Dark blue
67
+ pdf.cell(0, 10, "Interview Questions", ln=1)
68
+ pdf.ln(5)
69
+
70
+ # Process interview text
71
  pdf.set_font("Times", "", 12)
72
+ pdf.set_text_color(0, 0, 0) # Black
73
  interview_text = sanitize_text(interview_text)
74
+
75
+ # Format numbered questions
76
+ for line in interview_text.split('\n'):
77
+ line = line.strip()
78
+ if re.match(r'^\d+[\.\)]', line): # Detect numbered questions
79
+ pdf.set_font("Times", "B", 12)
80
+ pdf.multi_cell(0, 8, line)
81
+ pdf.set_font("Times", "", 12)
82
+ else:
83
+ pdf.multi_cell(0, 8, line)
84
+ pdf.ln(4)
85
+
86
+ # Recommendations section
87
+ pdf.add_page()
88
  pdf.set_font("Times", "B", 14)
89
+ pdf.set_text_color(0, 0, 139) # Dark blue
90
+ pdf.cell(0, 10, "Special Note & Recommendations", ln=1)
91
+ pdf.ln(8)
92
+
93
+ # Process recommendations
94
  pdf.set_font("Times", "", 12)
95
+ pdf.set_text_color(0, 0, 0) # Black
96
  note_text = sanitize_text(note_text)
97
+ pdf.multi_cell(0, 8, note_text)
98
+ pdf.ln(10)
99
+
100
+ # Creator credentials
101
+ pdf.set_font("Times", "I", 11)
102
+ pdf.set_text_color(0, 0, 128) # Navy blue
103
+ pdf.multi_cell(0, 8, "Prepared by:\nMahar Affandi Noor Ghazi\nPace GK Academy\nContact: +92 311 750 5369", align='C')
104
+
 
 
 
105
  filename = f"{name.replace(' ', '_')}_interview.pdf"
106
  pdf.output(filename)
107
  return filename
108
 
109
+ # Streamlit UI remains the same...
110
+ # (Keep your existing Streamlit interface code here without changes)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
112
+ # Rest of your Streamlit code remains identical...