File size: 8,041 Bytes
bb9e9c5 4d33dac bb9e9c5 4d33dac bb9e9c5 4d33dac bb9e9c5 4d33dac ab2cbe2 |
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 211 212 213 214 215 216 217 218 219 220 221 |
from flask import Flask, request, jsonify
from huggingface_hub import InferenceClient
from requests.exceptions import RequestException
app = Flask(__name__)
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.2")
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: "Choose either A, B, C, or D as the correct answer"
Explanation: ""
\n
[/INST]
"""
def generate_quiz(context):
formatted_prompt = system_instructions(context)
generate_kwargs = dict(
temperature=0.1,
max_new_tokens=2048,
top_p=0.95,
repetition_penalty=1.0,
do_sample=True,
seed=42,)
try:
response = client.text_generation(
formatted_prompt,
**generate_kwargs,
stream=False,
details=False,
return_full_text=False,
)
return response
except (RequestException, SystemExit) as e:
return {"error": str(e)}
@app.route("/", methods=["GET", "POST"])
def generate_quiz_page():
if request.method == "POST":
if request.content_type == 'application/json':
data = request.get_json()
context = data.get("context")
if context is None or context.strip() == "":
return jsonify({"error": "Missing or empty 'context' parameter"}), 400
else:
context = request.form.get("context")
if context is None or context.strip() == "":
return jsonify({"error": "Missing or empty 'context' parameter"}), 400
response = generate_quiz(context)
if request.content_type == 'application/json':
return jsonify(response)
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>
<style>
body {{
font-family: Arial, sans-serif;
background-color: #f0f0f0; /* Light grey background */
color: #333; /* Dark grey text color */
margin: 20px;
}}
h2 {{
background-color: #ffc107; /* Yellow background for heading */
padding: 10px;
border-radius: 5px;
}}
form {{
display: none; /* Hide the form after generating quiz */
}}
.quiz-container {{
background-color: #f0f0f0; /* Light grey background */
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); /* Light shadow effect */
margin-bottom: 20px;
}}
.quiz-container pre {{
white-space: pre-wrap; /* Preserve line breaks in quiz response */
}}
.generate-link {{
display: inline-block; /* Make the link inline-block */
margin-top: 10px;
border: 2px solid transparent; /* Transparent border initially */
padding: 5px 10px; /* Padding for spacing inside the border */
text-decoration: none; /* Remove underline */
color: #ffc107; /* Yellow text color */
font-weight: bold; /* Bold text */
border-radius: 4px;
background-color: #333; /* Dark grey background color */
}}
.generate-link:hover {{
text-decoration: none; /* Remove underline on hover */
background-color: #555; /* Darker grey background color on hover */
}}
a {{
color: #ffc107; /* Yellow text color for links */
text-decoration: none;
}}
a:hover {{
text-decoration: underline; /* Underline on hover */
}}
textarea {{
width: 100%;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
resize: vertical; /* Allow vertical resizing of textarea */
min-height: 150px; /* Minimum height for textarea */
}}
input[type="submit"] {{
background-color: #ffc107; /* Yellow background for submit button */
color: #333; /* Dark grey text color */
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 4px;
}}
input[type="submit"]:hover {{
background-color: #ffca28; /* Lighter yellow on hover */
}}
</style>
</head>
<body>
<div class="quiz-container">
<h2>Generated Quiz</h2>
<pre>{response}</pre>
<a href="/" class="generate-link">Back to generate another quiz</a>
</div>
</body>
</html>
"""
return quiz_html
# Default GET request handling
default_html = """
<!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>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f0f0f0; /* Light grey background */
color: #333; /* Dark grey text color */
margin: 20px;
}
h2 {
background-color: #ffc107; /* Yellow background for heading */
padding: 10px;
border-radius: 5px;
}
form {
background-color: #fff; /* White background for form */
padding: 20px;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); /* Light shadow effect */
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 10px;
}
textarea {
width: 98%;
padding: 10px;
border: 2px solid #ffc107; /* Yellow border */
border-radius: 4px;
resize: vertical; /* Allow vertical resizing of textarea */
min-height: 150px; /* Minimum height for textarea */
background-color: #f0f0f0; /* Light grey background */
}
input[type="submit"] {
background-color: #ffc107; /* Yellow background for submit button */
color: #333; /* Dark grey text color */
border: none;
padding: 10px 20px;
cursor: pointer;
border-radius: 4px;
}
input[type="submit"]:hover {
background-color: #ffca28; /* Lighter yellow on hover */
}
</style>
</head>
<body>
<h2>Generate Quiz</h2>
<form action="/" method="post">
<label for="context">Context:</label><br>
<textarea id="context" name="context" rows="20" required></textarea><br><br>
<input type="submit" value="Generate Quiz">
</form>
</body>
</html>
"""
return default_html
if __name__ == "__main__":
app.run(debug=True) |