Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
|
4 |
+
app = FastAPI()
|
5 |
+
|
6 |
+
|
7 |
+
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
8 |
+
|
9 |
+
def system_instructions(context):
|
10 |
+
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
|
11 |
+
Question: ""
|
12 |
+
Choices:
|
13 |
+
A): ""
|
14 |
+
B): ""
|
15 |
+
C): ""
|
16 |
+
D): ""
|
17 |
+
Answer: "A/B/C/D"
|
18 |
+
Explanation: ""
|
19 |
+
\n
|
20 |
+
[/INST]
|
21 |
+
"""
|
22 |
+
|
23 |
+
def generate_quiz(context):
|
24 |
+
formatted_prompt = system_instructions(context)
|
25 |
+
|
26 |
+
pre_prompt = [
|
27 |
+
{"role": "system", "content": formatted_prompt}
|
28 |
+
]
|
29 |
+
|
30 |
+
generate_kwargs = dict(
|
31 |
+
temperature=0.1,
|
32 |
+
max_new_tokens=2048,
|
33 |
+
top_p=0.95,
|
34 |
+
repetition_penalty=1.0,
|
35 |
+
do_sample=True,
|
36 |
+
seed=42,
|
37 |
+
)
|
38 |
+
|
39 |
+
response = client.text_generation(
|
40 |
+
formatted_prompt, **generate_kwargs, stream=False, details=False, return_full_text=False,
|
41 |
+
)
|
42 |
+
|
43 |
+
return response
|
44 |
+
|
45 |
+
@app.post("/generate-quiz")
|
46 |
+
async def generate_quiz_endpoint(context: str):
|
47 |
+
response = generate_quiz(context)
|
48 |
+
return {"quiz": response}
|