MCQA-Quiz / app.py
mou3az's picture
Create app.py
2e909de verified
raw
history blame
1.26 kB
from fastapi import FastAPI
from huggingface_hub import InferenceClient
app = FastAPI()
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.post("/generate-quiz")
async def generate_quiz_endpoint(context: str):
response = generate_quiz(context)
return {"quiz": response}