File size: 3,087 Bytes
1fcd25d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import request,jsonify,Flask

from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

app = Flask(__name__)

path_model ="./"
tokenizer = AutoTokenizer.from_pretrained(path_model)
model = AutoModelForCausalLM.from_pretrained(path_model, torch_dtype=torch.float32)

def generate_qa_summary(topic, num_questions, temperature=0.4):
    device = "cuda" if torch.cuda.is_available() else "cpu"
    model.to(device)
    questions_and_answers = []
    while len(questions_and_answers) < num_questions:
        prompt = f"{topic} Question:\n"
        input_ids = tokenizer.encode(prompt, return_tensors="pt").to(device)
        output = model.generate(
            max_new_tokens=1000,
            #length_penalty=1.0,
            early_stopping=True,
            input_ids=input_ids,
            num_return_sequences=1,
            temperature=temperature,
            no_repeat_ngram_size=4,
            pad_token_id=tokenizer.eos_token_id,
            top_p=0.80,
            do_sample=True,
        )
        response = tokenizer.decode(output[0], skip_special_tokens=True)
        # Check if the response is valid (contains both 'Question:' and 'Answer:')
        if "Question:" in response and "Answer:" in response:
            questions_and_answers.append(response)
    return questions_and_answers

@app.route("/api/Gen", methods=["POST"])
def generate_questions1():
    data = request.get_json()
    course_name = data["courseName"]
    num_questions = int(data["numQuestions"])
    qa_summary = generate_qa_summary(course_name, num_questions)
    if not qa_summary:
        return jsonify({"error": "Failed to generate any questions"})
    first_item = qa_summary[0]
    topic, _ = (
        first_item.split("Question:", maxsplit=1)
        if "Question:" in first_item
        else (first_item, "")
    )
    topic = topic.strip()
    formatted_summaries = [f"<strong>{topic}:</strong><br><br>"]  # Start with the topic
    for index, item in enumerate(qa_summary):
        if "Question:" not in item:
            item = f"Question: {item}"  # Prepend "Question:" if missing
        else:
            parts = item.split("Question:")
            item = "Question:" + " ".join(
                parts[1:]
            )  # Reassemble without extra "Question:"

        _, question_answer = item.split("Question:", maxsplit=1)
        question, answer = (
            question_answer.split("Answer:", maxsplit=1)
            if "Answer:" in question_answer
            else (question_answer, "No answer provided.")
        )
        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>"
        formatted_summaries.append(formatted_question_answer)
    return jsonify(formatted_summaries)


if __name__ == "__main__":
    app.run(debug=True)