File size: 5,068 Bytes
20d8f9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch


import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

MODEL_ID = "Fayza38/Question_and_Answer"

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)

model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    torch_dtype=torch.float32,
    device_map="cpu"
)

def generate_questions(prompt):

    messages = [
        {"role": "system", "content": "You are a professional interview question generator."},
        {"role": "user", "content": prompt}
    ]

    formatted_prompt = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True
    )

    inputs = tokenizer(formatted_prompt, return_tensors="pt")

    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=800,
            temperature=0.7
        )

    decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)

    return decoded


iface = gr.Interface(
    fn=generate_questions,
    inputs="text",
    outputs="text",
    title="Interview Question Generator"
)

iface.launch()

# # =========================================
# # ENUM MAPPINGS (Match Backend Enums)
# # =========================================

# SESSION_TYPES = {
#     1: "technical",
#     2: "softskills"
# }

# TRACKS = {
#     19: "generalprogramming"
# }

# # =========================================
# # LOAD MODEL ONCE (Global)
# # =========================================

# MODEL_PATH = "Fayza38/Question_and_Answer"

# tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)

# model = AutoModelForCausalLM.from_pretrained(
#     MODEL_PATH,
#     torch_dtype=torch.float32,
#     device_map="cpu"
# )

# app = FastAPI()


# # =========================================
# # REQUEST MODEL
# # =========================================

# class QuestionRequest(BaseModel):
#     sessionType: int
#     difficultyLevel: int | None = None
#     trackName: int


# # =========================================
# # HELPER: GENERATE TEXT USING QWEN TEMPLATE
# # =========================================

# def generate_from_model(prompt: str):

#     messages = [
#         {"role": "system", "content": "You are a professional interview question generator."},
#         {"role": "user", "content": prompt}
#     ]

#     formatted_prompt = tokenizer.apply_chat_template(
#         messages,
#         tokenize=False,
#         add_generation_prompt=True
#     )

#     inputs = tokenizer(formatted_prompt, return_tensors="pt")

#     with torch.no_grad():
#         outputs = model.generate(
#             **inputs,
#             max_new_tokens=1200,
#             temperature=0.7
#         )

#     decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)

#     return decoded


# # =========================================
# # PARSE Q/A FORMAT
# # =========================================

# def parse_qa_blocks(text: str):

#     blocks = text.split("\n\n")
#     results = []

#     for block in blocks:
#         if "Q:" in block and "A:" in block:
#             parts = block.split("A:")
#             question = parts[0].replace("Q:", "").strip()
#             answer = parts[1].strip()
#             results.append((question, answer))

#     return results




# # =========================================
# # MAIN ENDPOINT
# # =========================================

# @app.post("/generate-questions")
# def generate_questions(request: QuestionRequest):

#     if request.sessionType not in SESSION_TYPES:
#         raise HTTPException(status_code=400, detail="Invalid session type")

#     session_type = SESSION_TYPES[request.sessionType]

#     # ---------------- SOFT SKILLS ----------------

#     if session_type == "softskills":

#         prompt = """
# Generate 10 behavioral interview questions.
# Format exactly as:
# Q: ...
# A: ...
# """

#     # ---------------- TECHNICAL ----------------

#     elif session_type == "technical":

#         if request.trackName not in TRACKS:
#             raise HTTPException(status_code=400, detail="Track not supported")

#         difficulty = request.difficultyLevel or 1

#         prompt = f"""
# Generate 10 General Programming interview questions.
# Difficulty level: {difficulty}
# Format exactly as:
# Q: ...
# A: ...
# """

#     else:
#         raise HTTPException(status_code=400, detail="Invalid session type")

#     # -------- Generate once --------
#     raw_output = generate_from_model(prompt)

#     qa_pairs = parse_qa_blocks(raw_output)

#     if len(qa_pairs) == 0:
#         raise HTTPException(status_code=500, detail="Model failed to generate valid Q/A format")

#     response = []

#     for idx, (question, answer) in enumerate(qa_pairs[:10], 1):

#         response.append({
#             "questionText": question,
#             "questionId": idx,
#             "questionIdealAnswer": answer
#         })

#     return response