Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
import os
|
| 4 |
+
import requests
|
| 5 |
+
from fpdf import FPDF
|
| 6 |
+
|
| 7 |
+
# Function to generate interview questions using Gemini API
|
| 8 |
+
def generate_interview(data):
|
| 9 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
| 10 |
+
if not api_key:
|
| 11 |
+
return "β GEMINI_API_KEY is not set. Please add it in the Hugging Face 'Secrets'."
|
| 12 |
+
|
| 13 |
+
prompt = f"""
|
| 14 |
+
Generate a comprehensive mock interview for a candidate with the following details:
|
| 15 |
+
|
| 16 |
+
- Name: {data['name']}
|
| 17 |
+
- Father's Name: {data['father_name']}
|
| 18 |
+
- District of Domicile: {data['district']}
|
| 19 |
+
- Degrees: {data['degrees']}
|
| 20 |
+
- Subject: {data['subject']}
|
| 21 |
+
- Department Preparing For: {data['department']}
|
| 22 |
+
- Hobby: {data['hobby']}
|
| 23 |
+
- Favorite Personality: {data['favorite_personality']}
|
| 24 |
+
|
| 25 |
+
The interview should include:
|
| 26 |
+
- Questions about the candidate's name and any notable personalities with the same name.
|
| 27 |
+
- Questions related to the candidate's district, including famous personalities, places, demographics, and administrative issues.
|
| 28 |
+
- Questions pertaining to the candidate's degrees and subject matter.
|
| 29 |
+
- Questions about the department the candidate is preparing for.
|
| 30 |
+
- General knowledge questions covering geography, national and international issues, sports, and recent government projects in Pakistan related to the candidate's field.
|
| 31 |
+
- Situational and behavioral questions.
|
| 32 |
+
|
| 33 |
+
Provide the questions in a numbered list format.
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
headers = {
|
| 37 |
+
"Content-Type": "application/json",
|
| 38 |
+
"Authorization": f"Bearer {api_key}"
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
payload = {
|
| 42 |
+
"contents": [{"parts": [{"text": prompt}]}]
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
response = requests.post(
|
| 46 |
+
"https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent",
|
| 47 |
+
headers=headers,
|
| 48 |
+
json=payload
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if response.status_code == 200:
|
| 52 |
+
result = response.json()
|
| 53 |
+
try:
|
| 54 |
+
return result['candidates'][0]['content']['parts'][0]['text']
|
| 55 |
+
except (KeyError, IndexError):
|
| 56 |
+
return "β Unexpected response format from Gemini API."
|
| 57 |
+
else:
|
| 58 |
+
return f"β Error {response.status_code}: {response.text}"
|
| 59 |
+
|
| 60 |
+
# Function to create a PDF from the interview text
|
| 61 |
+
def create_pdf(interview_text, candidate_name):
|
| 62 |
+
pdf = FPDF()
|
| 63 |
+
pdf.add_page()
|
| 64 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 65 |
+
pdf.set_font("Arial", size=12)
|
| 66 |
+
lines = interview_text.split('\n')
|
| 67 |
+
for line in lines:
|
| 68 |
+
pdf.multi_cell(0, 10, line)
|
| 69 |
+
filename = f"{candidate_name.replace(' ', '_')}_Interview.pdf"
|
| 70 |
+
pdf.output(filename)
|
| 71 |
+
return filename
|
| 72 |
+
|
| 73 |
+
# Streamlit App
|
| 74 |
+
st.set_page_config(page_title="Mock Interview Generator", layout="centered")
|
| 75 |
+
st.title("π Mock Interview Generator for Pakistani Competitive Exams")
|
| 76 |
+
|
| 77 |
+
with st.form("interview_form"):
|
| 78 |
+
name = st.text_input("Full Name")
|
| 79 |
+
father_name = st.text_input("Father's Name")
|
| 80 |
+
district = st.text_input("District of Domicile")
|
| 81 |
+
degrees = st.text_input("Degrees (e.g., BSc, MSc)")
|
| 82 |
+
subject = st.text_input("Subject")
|
| 83 |
+
department = st.text_input("Department Preparing For")
|
| 84 |
+
hobby = st.text_input("Hobby")
|
| 85 |
+
favorite_personality = st.text_input("Favorite Personality")
|
| 86 |
+
submitted = st.form_submit_button("Generate Interview")
|
| 87 |
+
|
| 88 |
+
if submitted:
|
| 89 |
+
if all([name, father_name, district, degrees, subject, department, hobby, favorite_personality]):
|
| 90 |
+
data = {
|
| 91 |
+
"name": name,
|
| 92 |
+
"father_name": father_name,
|
| 93 |
+
"district": district,
|
| 94 |
+
"degrees": degrees,
|
| 95 |
+
"subject": subject,
|
| 96 |
+
"department": department,
|
| 97 |
+
"hobby": hobby,
|
| 98 |
+
"favorite_personality": favorite_personality
|
| 99 |
+
}
|
| 100 |
+
with st.spinner("Generating interview..."):
|
| 101 |
+
interview = generate_interview(data)
|
| 102 |
+
st.success("β
Interview Generated!")
|
| 103 |
+
st.text_area("π Interview Questions", interview, height=400)
|
| 104 |
+
pdf_file = create_pdf(interview, name)
|
| 105 |
+
with open(pdf_file, "rb") as file:
|
| 106 |
+
st.download_button(
|
| 107 |
+
label="π₯ Download Interview as PDF",
|
| 108 |
+
data=file,
|
| 109 |
+
file_name=pdf_file,
|
| 110 |
+
mime="application/pdf"
|
| 111 |
+
)
|
| 112 |
+
else:
|
| 113 |
+
st.warning("Please fill in all the fields.")
|