Fayza38 commited on
Commit
20d8f9c
·
verified ·
1 Parent(s): f773724

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +210 -161
app.py CHANGED
@@ -1,161 +1,210 @@
1
- from fastapi import FastAPI, HTTPException
2
- from pydantic import BaseModel
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
4
- from TextToSpeech import text_to_speech
5
- import torch
6
- import base64
7
-
8
- # =========================================
9
- # ENUM MAPPINGS (Match Backend Enums)
10
- # =========================================
11
-
12
- SESSION_TYPES = {
13
- 1: "technical",
14
- 2: "softskills"
15
- }
16
-
17
- TRACKS = {
18
- 19: "generalprogramming"
19
- }
20
-
21
- # =========================================
22
- # LOAD MODEL ONCE (Global)
23
- # =========================================
24
-
25
- MODEL_PATH = "Fayza38/Question_and_Answer"
26
-
27
- tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
28
-
29
- model = AutoModelForCausalLM.from_pretrained(
30
- MODEL_PATH,
31
- torch_dtype=torch.float32,
32
- device_map="cpu"
33
- )
34
-
35
- app = FastAPI()
36
-
37
-
38
- # =========================================
39
- # REQUEST MODEL
40
- # =========================================
41
-
42
- class QuestionRequest(BaseModel):
43
- sessionType: int
44
- difficultyLevel: int | None = None
45
- trackName: int
46
-
47
-
48
- # =========================================
49
- # HELPER: GENERATE TEXT USING QWEN TEMPLATE
50
- # =========================================
51
-
52
- def generate_from_model(prompt: str):
53
-
54
- messages = [
55
- {"role": "system", "content": "You are a professional interview question generator."},
56
- {"role": "user", "content": prompt}
57
- ]
58
-
59
- formatted_prompt = tokenizer.apply_chat_template(
60
- messages,
61
- tokenize=False,
62
- add_generation_prompt=True
63
- )
64
-
65
- inputs = tokenizer(formatted_prompt, return_tensors="pt")
66
-
67
- with torch.no_grad():
68
- outputs = model.generate(
69
- **inputs,
70
- max_new_tokens=1200,
71
- temperature=0.7
72
- )
73
-
74
- decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
75
-
76
- return decoded
77
-
78
-
79
- # =========================================
80
- # PARSE Q/A FORMAT
81
- # =========================================
82
-
83
- def parse_qa_blocks(text: str):
84
-
85
- blocks = text.split("\n\n")
86
- results = []
87
-
88
- for block in blocks:
89
- if "Q:" in block and "A:" in block:
90
- parts = block.split("A:")
91
- question = parts[0].replace("Q:", "").strip()
92
- answer = parts[1].strip()
93
- results.append((question, answer))
94
-
95
- return results
96
-
97
-
98
-
99
-
100
- # =========================================
101
- # MAIN ENDPOINT
102
- # =========================================
103
-
104
- @app.post("/generate-questions")
105
- def generate_questions(request: QuestionRequest):
106
-
107
- if request.sessionType not in SESSION_TYPES:
108
- raise HTTPException(status_code=400, detail="Invalid session type")
109
-
110
- session_type = SESSION_TYPES[request.sessionType]
111
-
112
- # ---------------- SOFT SKILLS ----------------
113
-
114
- if session_type == "softskills":
115
-
116
- prompt = """
117
- Generate 10 behavioral interview questions.
118
- Format exactly as:
119
- Q: ...
120
- A: ...
121
- """
122
-
123
- # ---------------- TECHNICAL ----------------
124
-
125
- elif session_type == "technical":
126
-
127
- if request.trackName not in TRACKS:
128
- raise HTTPException(status_code=400, detail="Track not supported")
129
-
130
- difficulty = request.difficultyLevel or 1
131
-
132
- prompt = f"""
133
- Generate 10 General Programming interview questions.
134
- Difficulty level: {difficulty}
135
- Format exactly as:
136
- Q: ...
137
- A: ...
138
- """
139
-
140
- else:
141
- raise HTTPException(status_code=400, detail="Invalid session type")
142
-
143
- # -------- Generate once --------
144
- raw_output = generate_from_model(prompt)
145
-
146
- qa_pairs = parse_qa_blocks(raw_output)
147
-
148
- if len(qa_pairs) == 0:
149
- raise HTTPException(status_code=500, detail="Model failed to generate valid Q/A format")
150
-
151
- response = []
152
-
153
- for idx, (question, answer) in enumerate(qa_pairs[:10], 1):
154
-
155
- response.append({
156
- "questionText": question,
157
- "questionId": idx,
158
- "questionIdealAnswer": answer
159
- })
160
-
161
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import torch
5
+
6
+
7
+ import gradio as gr
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer
9
+ import torch
10
+
11
+ MODEL_ID = "Fayza38/Question_and_Answer"
12
+
13
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
14
+
15
+ model = AutoModelForCausalLM.from_pretrained(
16
+ MODEL_ID,
17
+ torch_dtype=torch.float32,
18
+ device_map="cpu"
19
+ )
20
+
21
+ def generate_questions(prompt):
22
+
23
+ messages = [
24
+ {"role": "system", "content": "You are a professional interview question generator."},
25
+ {"role": "user", "content": prompt}
26
+ ]
27
+
28
+ formatted_prompt = tokenizer.apply_chat_template(
29
+ messages,
30
+ tokenize=False,
31
+ add_generation_prompt=True
32
+ )
33
+
34
+ inputs = tokenizer(formatted_prompt, return_tensors="pt")
35
+
36
+ with torch.no_grad():
37
+ outputs = model.generate(
38
+ **inputs,
39
+ max_new_tokens=800,
40
+ temperature=0.7
41
+ )
42
+
43
+ decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
44
+
45
+ return decoded
46
+
47
+
48
+ iface = gr.Interface(
49
+ fn=generate_questions,
50
+ inputs="text",
51
+ outputs="text",
52
+ title="Interview Question Generator"
53
+ )
54
+
55
+ iface.launch()
56
+
57
+ # # =========================================
58
+ # # ENUM MAPPINGS (Match Backend Enums)
59
+ # # =========================================
60
+
61
+ # SESSION_TYPES = {
62
+ # 1: "technical",
63
+ # 2: "softskills"
64
+ # }
65
+
66
+ # TRACKS = {
67
+ # 19: "generalprogramming"
68
+ # }
69
+
70
+ # # =========================================
71
+ # # LOAD MODEL ONCE (Global)
72
+ # # =========================================
73
+
74
+ # MODEL_PATH = "Fayza38/Question_and_Answer"
75
+
76
+ # tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
77
+
78
+ # model = AutoModelForCausalLM.from_pretrained(
79
+ # MODEL_PATH,
80
+ # torch_dtype=torch.float32,
81
+ # device_map="cpu"
82
+ # )
83
+
84
+ # app = FastAPI()
85
+
86
+
87
+ # # =========================================
88
+ # # REQUEST MODEL
89
+ # # =========================================
90
+
91
+ # class QuestionRequest(BaseModel):
92
+ # sessionType: int
93
+ # difficultyLevel: int | None = None
94
+ # trackName: int
95
+
96
+
97
+ # # =========================================
98
+ # # HELPER: GENERATE TEXT USING QWEN TEMPLATE
99
+ # # =========================================
100
+
101
+ # def generate_from_model(prompt: str):
102
+
103
+ # messages = [
104
+ # {"role": "system", "content": "You are a professional interview question generator."},
105
+ # {"role": "user", "content": prompt}
106
+ # ]
107
+
108
+ # formatted_prompt = tokenizer.apply_chat_template(
109
+ # messages,
110
+ # tokenize=False,
111
+ # add_generation_prompt=True
112
+ # )
113
+
114
+ # inputs = tokenizer(formatted_prompt, return_tensors="pt")
115
+
116
+ # with torch.no_grad():
117
+ # outputs = model.generate(
118
+ # **inputs,
119
+ # max_new_tokens=1200,
120
+ # temperature=0.7
121
+ # )
122
+
123
+ # decoded = tokenizer.decode(outputs[0], skip_special_tokens=True)
124
+
125
+ # return decoded
126
+
127
+
128
+ # # =========================================
129
+ # # PARSE Q/A FORMAT
130
+ # # =========================================
131
+
132
+ # def parse_qa_blocks(text: str):
133
+
134
+ # blocks = text.split("\n\n")
135
+ # results = []
136
+
137
+ # for block in blocks:
138
+ # if "Q:" in block and "A:" in block:
139
+ # parts = block.split("A:")
140
+ # question = parts[0].replace("Q:", "").strip()
141
+ # answer = parts[1].strip()
142
+ # results.append((question, answer))
143
+
144
+ # return results
145
+
146
+
147
+
148
+
149
+ # # =========================================
150
+ # # MAIN ENDPOINT
151
+ # # =========================================
152
+
153
+ # @app.post("/generate-questions")
154
+ # def generate_questions(request: QuestionRequest):
155
+
156
+ # if request.sessionType not in SESSION_TYPES:
157
+ # raise HTTPException(status_code=400, detail="Invalid session type")
158
+
159
+ # session_type = SESSION_TYPES[request.sessionType]
160
+
161
+ # # ---------------- SOFT SKILLS ----------------
162
+
163
+ # if session_type == "softskills":
164
+
165
+ # prompt = """
166
+ # Generate 10 behavioral interview questions.
167
+ # Format exactly as:
168
+ # Q: ...
169
+ # A: ...
170
+ # """
171
+
172
+ # # ---------------- TECHNICAL ----------------
173
+
174
+ # elif session_type == "technical":
175
+
176
+ # if request.trackName not in TRACKS:
177
+ # raise HTTPException(status_code=400, detail="Track not supported")
178
+
179
+ # difficulty = request.difficultyLevel or 1
180
+
181
+ # prompt = f"""
182
+ # Generate 10 General Programming interview questions.
183
+ # Difficulty level: {difficulty}
184
+ # Format exactly as:
185
+ # Q: ...
186
+ # A: ...
187
+ # """
188
+
189
+ # else:
190
+ # raise HTTPException(status_code=400, detail="Invalid session type")
191
+
192
+ # # -------- Generate once --------
193
+ # raw_output = generate_from_model(prompt)
194
+
195
+ # qa_pairs = parse_qa_blocks(raw_output)
196
+
197
+ # if len(qa_pairs) == 0:
198
+ # raise HTTPException(status_code=500, detail="Model failed to generate valid Q/A format")
199
+
200
+ # response = []
201
+
202
+ # for idx, (question, answer) in enumerate(qa_pairs[:10], 1):
203
+
204
+ # response.append({
205
+ # "questionText": question,
206
+ # "questionId": idx,
207
+ # "questionIdealAnswer": answer
208
+ # })
209
+
210
+ # return response