File size: 1,994 Bytes
ac035ed
 
 
0d9e617
 
 
 
 
 
 
ac035ed
 
0d9e617
ac035ed
 
0d9e617
9274b84
0d9e617
9274b84
 
 
 
0d9e617
9274b84
0d9e617
ac035ed
0d9e617
 
ac035ed
 
 
 
 
 
 
0d9e617
 
 
 
 
 
 
15a9593
0d9e617
 
 
 
 
 
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
from langchain.prompts import ChatPromptTemplate
from langchain.chains import LLMChain
from langchain_community.llms import LlamaCpp
from huggingface_hub import hf_hub_download

hf_hub_download(
    repo_id="bartowski/Meta-Llama-3.1-8B-Instruct-GGUF",
    filename="Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf",
    local_dir = "./models"
)

llm = LlamaCpp(
    model_path="models/Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf",
    )

# Generator function
def gen_quiz(text_input,num):
    prompt = ChatPromptTemplate.from_template(
        "Generate {num} questions and their correct answers based on the following text:\n\n{text}\n\n"
    )
    # Prepare the inputs for the chain
    prompt_input = {"text": text_input, "num": num}
    chain = LLMChain(llm=llm, prompt=prompt)    
    quiz = chain(prompt_input)
    return quiz['text']

# Example
text_example = "In general, IFC, or “Industry Foundation Classes”, \
is a standardized, digital description of the built environment, \
including buildings and civil infrastructure. \
It is an open, international standard (ISO 16739-1:2018), \
meant to be vendor-neutral, or agnostic, and usable across a wide range of hardware devices, \
software platforms, and interfaces for many different use cases. \
The IFC schema specification is the primary technical deliverable of buildingSMART International to fulfill its goal to promote openBIM."


import gradio as gr

# Gradio Interface
gr.close_all()
demo = gr.Interface(fn=gen_quiz, 
                    inputs=[gr.Textbox(label="Text to generate quiz from", lines=6),
                            gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Quiz")],
                    outputs=[gr.Textbox(label="Result", lines=10)],
                    examples=[[text_example, 3]],
                    title="Quiz Generator with LlamaCpp",
                    description="Generating quiz based on given texts using Meta-Llama-3.1-8B-Instruct-Q5_K_M.gguf!"
                   )
demo.launch()