Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- app.py +74 -3
- config.json +39 -0
- merges.txt +0 -0
- model.safetensors +3 -0
- requirements.txt +0 -0
- tokenizer_config.json +22 -0
- vocab.json +0 -0
app.py
CHANGED
|
@@ -1,3 +1,74 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import request,jsonify,Flask
|
| 2 |
+
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
app = Flask(__name__)
|
| 7 |
+
|
| 8 |
+
path_model ="./"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(path_model)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(path_model, torch_dtype=torch.float32)
|
| 11 |
+
|
| 12 |
+
def generate_qa_summary(topic, num_questions, temperature=0.4):
|
| 13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 14 |
+
model.to(device)
|
| 15 |
+
questions_and_answers = []
|
| 16 |
+
while len(questions_and_answers) < num_questions:
|
| 17 |
+
prompt = f"{topic} Question:\n"
|
| 18 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
|
| 19 |
+
output = model.generate(
|
| 20 |
+
max_new_tokens=1000,
|
| 21 |
+
#length_penalty=1.0,
|
| 22 |
+
early_stopping=True,
|
| 23 |
+
input_ids=input_ids,
|
| 24 |
+
num_return_sequences=1,
|
| 25 |
+
temperature=temperature,
|
| 26 |
+
no_repeat_ngram_size=4,
|
| 27 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 28 |
+
top_p=0.80,
|
| 29 |
+
do_sample=True,
|
| 30 |
+
)
|
| 31 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 32 |
+
# Check if the response is valid (contains both 'Question:' and 'Answer:')
|
| 33 |
+
if "Question:" in response and "Answer:" in response:
|
| 34 |
+
questions_and_answers.append(response)
|
| 35 |
+
return questions_and_answers
|
| 36 |
+
|
| 37 |
+
@app.route("/api/Gen", methods=["POST"])
|
| 38 |
+
def generate_questions1():
|
| 39 |
+
data = request.get_json()
|
| 40 |
+
course_name = data["courseName"]
|
| 41 |
+
num_questions = int(data["numQuestions"])
|
| 42 |
+
qa_summary = generate_qa_summary(course_name, num_questions)
|
| 43 |
+
if not qa_summary:
|
| 44 |
+
return jsonify({"error": "Failed to generate any questions"})
|
| 45 |
+
first_item = qa_summary[0]
|
| 46 |
+
topic, _ = (
|
| 47 |
+
first_item.split("Question:", maxsplit=1)
|
| 48 |
+
if "Question:" in first_item
|
| 49 |
+
else (first_item, "")
|
| 50 |
+
)
|
| 51 |
+
topic = topic.strip()
|
| 52 |
+
formatted_summaries = [f"<strong>{topic}:</strong><br><br>"] # Start with the topic
|
| 53 |
+
for index, item in enumerate(qa_summary):
|
| 54 |
+
if "Question:" not in item:
|
| 55 |
+
item = f"Question: {item}" # Prepend "Question:" if missing
|
| 56 |
+
else:
|
| 57 |
+
parts = item.split("Question:")
|
| 58 |
+
item = "Question:" + " ".join(
|
| 59 |
+
parts[1:]
|
| 60 |
+
) # Reassemble without extra "Question:"
|
| 61 |
+
|
| 62 |
+
_, question_answer = item.split("Question:", maxsplit=1)
|
| 63 |
+
question, answer = (
|
| 64 |
+
question_answer.split("Answer:", maxsplit=1)
|
| 65 |
+
if "Answer:" in question_answer
|
| 66 |
+
else (question_answer, "No answer provided.")
|
| 67 |
+
)
|
| 68 |
+
formatted_question_answer = f"<div class='question-container'><div>-Question: {question.strip()}<br<button onclick='toggleAnswer({index})'><i id='icon{index}' class='fas fa-eye fa'></i></button></div><div id='answer{index}' style='display:none;'>-Answer: {answer.strip()}</div></div><br></div>"
|
| 69 |
+
formatted_summaries.append(formatted_question_answer)
|
| 70 |
+
return jsonify(formatted_summaries)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
app.run(debug=True)
|
config.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_name_or_path": "gpt2",
|
| 3 |
+
"activation_function": "gelu_new",
|
| 4 |
+
"architectures": [
|
| 5 |
+
"GPT2LMHeadModel"
|
| 6 |
+
],
|
| 7 |
+
"attn_pdrop": 0.1,
|
| 8 |
+
"bos_token_id": 50256,
|
| 9 |
+
"embd_pdrop": 0.1,
|
| 10 |
+
"eos_token_id": 50256,
|
| 11 |
+
"initializer_range": 0.02,
|
| 12 |
+
"layer_norm_epsilon": 1e-05,
|
| 13 |
+
"model_type": "gpt2",
|
| 14 |
+
"n_ctx": 1024,
|
| 15 |
+
"n_embd": 768,
|
| 16 |
+
"n_head": 12,
|
| 17 |
+
"n_inner": null,
|
| 18 |
+
"n_layer": 12,
|
| 19 |
+
"n_positions": 1024,
|
| 20 |
+
"reorder_and_upcast_attn": false,
|
| 21 |
+
"resid_pdrop": 0.1,
|
| 22 |
+
"scale_attn_by_inverse_layer_idx": false,
|
| 23 |
+
"scale_attn_weights": true,
|
| 24 |
+
"summary_activation": null,
|
| 25 |
+
"summary_first_dropout": 0.1,
|
| 26 |
+
"summary_proj_to_labels": true,
|
| 27 |
+
"summary_type": "cls_index",
|
| 28 |
+
"summary_use_proj": true,
|
| 29 |
+
"task_specific_params": {
|
| 30 |
+
"text-generation": {
|
| 31 |
+
"do_sample": true,
|
| 32 |
+
"max_length": 50
|
| 33 |
+
}
|
| 34 |
+
},
|
| 35 |
+
"torch_dtype": "float32",
|
| 36 |
+
"transformers_version": "4.38.2",
|
| 37 |
+
"use_cache": true,
|
| 38 |
+
"vocab_size": 50257
|
| 39 |
+
}
|
merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c888c10456369529f0782ddff007e9730f63ceefffb57c20b08b3898ca9d117a
|
| 3 |
+
size 497774208
|
requirements.txt
ADDED
|
Binary file (1.13 kB). View file
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_bos_token": false,
|
| 3 |
+
"add_prefix_space": false,
|
| 4 |
+
"added_tokens_decoder": {
|
| 5 |
+
"50256": {
|
| 6 |
+
"content": "<|endoftext|>",
|
| 7 |
+
"lstrip": false,
|
| 8 |
+
"normalized": true,
|
| 9 |
+
"rstrip": false,
|
| 10 |
+
"single_word": false,
|
| 11 |
+
"special": true
|
| 12 |
+
}
|
| 13 |
+
},
|
| 14 |
+
"bos_token": "<|endoftext|>",
|
| 15 |
+
"clean_up_tokenization_spaces": true,
|
| 16 |
+
"eos_token": "<|endoftext|>",
|
| 17 |
+
"errors": "replace",
|
| 18 |
+
"model_max_length": 1024,
|
| 19 |
+
"pad_token": "<|endoftext|>",
|
| 20 |
+
"tokenizer_class": "GPT2Tokenizer",
|
| 21 |
+
"unk_token": "<|endoftext|>"
|
| 22 |
+
}
|
vocab.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|