File size: 2,498 Bytes
f8bdf3d
2e909de
 
f8bdf3d
2e909de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8bdf3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9679550
 
 
 
 
 
 
 
 
 
f8bdf3d
9679550
 
 
 
 
 
 
 
f8bdf3d
 
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
from flask import Flask, render_template, request, jsonify
from huggingface_hub import InferenceClient

app = Flask(__name__)


client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")

def system_instructions(context):
    return f"""<s> [INST] Your are a great teacher and your task is to create 6 questions with answer and 4 choices based on the following context:\n\n{context}\n\n. Each example should be like this
    Question: ""
    Choices:
    A): ""
    B): ""
    C): ""
    D): ""
    Answer: "A/B/C/D"
    Explanation: ""
    \n
    [/INST]
    """

def generate_quiz(context):
        formatted_prompt = system_instructions(context)

        pre_prompt = [
            {"role": "system", "content": formatted_prompt}
        ]

        generate_kwargs = dict(
            temperature=0.1,
            max_new_tokens=2048,
            top_p=0.95,
            repetition_penalty=1.0,
            do_sample=True,
            seed=42,
        )

        response = client.text_generation(
            formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False,
        )
        
        return response

@app.route("/", methods=["GET", "POST"])
def generate_quiz_page():
    if request.method == "POST":
        context = request.form.get("context")
        response = generate_quiz(context)
        
        quiz_html = f"""
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Generated Quiz</title>
        </head>
        <body>
            <h2>Generated Quiz</h2>
            <pre>{response}</pre>
            <p><a href="/">Back to generate another quiz</a></p>
        </body>
        </html>
        """
        
        return quiz_html
    
    # Default GET request handling
    return """
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Generate Quiz</title>
    </head>
    <body>
        <h2>Generate Quiz</h2>
        <form action="/" method="post">
            <label for="context">Context:</label><br>
            <textarea id="context" name="context" rows="4" cols="50" required></textarea><br><br>
            <input type="submit" value="Generate Quiz">
        </form>
    </body>
    </html>
    """

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