File size: 4,218 Bytes
a5bc9f7 af93137 f95926d ab5c901 f8d9c9f ab5c901 af93137 c1162bf f95926d ab5c901 6ee1ed5 f8d9c9f af93137 6e8eb29 f95926d b7d4791 d28e2fc 14e10ce 6e8eb29 f95926d fa2ae91 f95926d fa2ae91 f95926d fa2ae91 f95926d fa2ae91 f95926d f8d9c9f f95926d a08aae2 f95926d 14e10ce 6ee1ed5 |
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 |
import gradio as gr
from quiz import *
from chatbot import chat
from llama_cpp import Llama
from huggingface_hub import hf_hub_download
from functools import partial
# Load model (unchanged)
model_path = hf_hub_download(
repo_id="ebbalg/llama-finetome",
filename="llama-3.2-1b-instruct.Q4_K_M.gguf"
)
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=2, verbose=False, chat_format="llama-3")
raw_quiz = create_quiz(llm)
parsed_quiz = parse_quiz(raw_quiz)
with gr.Blocks(title="TAI: AI Teacher Assistant") as demo:
gr.Markdown("""
# TAI: Your AI Teacher Assistant
Ask questions about AI and Machine Learning! Test your understanding with a quiz.
""")
#gr.Markdown(raw_quiz)
#gr.Markdown(str(parsed_quiz[0]))
with gr.Row():
# Chat column
with gr.Column(scale=1):
gr.Markdown("## Ask me anything")
gr.ChatInterface(
fn=lambda message, history: chat(llm, message, history),
examples=[
"Which tasks can recurrent neural networks address?",
"Explain backpropagation in simple terms.",
"What are the benefits of regularization during training?",
"Write a short summary of advancements that allowed for deep neural networks to work."
]
)
# Quiz column
with gr.Column(scale=2):
gr.Markdown("## Test Yourself")
start_btn = gr.Button("Start Quiz", variant="primary")
question_md = gr.Markdown("")
feedback_md = gr.Markdown("")
progress_md = gr.Markdown("")
# Answer buttons
with gr.Row():
btn_A = gr.Button("A", visible=False)
btn_B = gr.Button("B", visible=False)
btn_C = gr.Button("C", visible=False)
btn_D = gr.Button("D", visible=False)
retry_btn = gr.Button("Retry", visible=False)
# States
idx_state = gr.State(0)
score_state = gr.State(0)
# Start quiz
def start_quiz_ui():
q_text, feedback, progress, idx, score = start_quiz(parsed_quiz)
return (
q_text, feedback, progress, idx, score,
gr.update(visible=False), # hide start
gr.update(visible=True), # show A
gr.update(visible=True), # show B
gr.update(visible=True), # show C
gr.update(visible=True), # show D
gr.update(visible=True) # show retry
)
start_btn.click(
fn=start_quiz_ui,
inputs=[],
outputs=[question_md, feedback_md, progress_md, idx_state, score_state,
start_btn, btn_A, btn_B, btn_C, btn_D, retry_btn]
)
# Answer button clicks
for letter, btn in zip(["A", "B", "C", "D"], [btn_A, btn_B, btn_C, btn_D]):
btn_fn = partial(answer_question, parsed_quiz, letter)
btn.click(
fn=btn_fn,
inputs=[idx_state, score_state],
outputs=[question_md, idx_state, score_state, feedback_md, progress_md]
)
# Retry button
def retry_quiz_ui():
q_text, feedback, progress, idx, score = start_quiz(parsed_quiz)
return (
q_text, feedback, progress, idx, score,
gr.update(visible=True), # show start
gr.update(visible=False), # hide A
gr.update(visible=False), # hide B
gr.update(visible=False), # hide C
gr.update(visible=False), # hide D
gr.update(visible=False) # hide retry
)
retry_btn.click(
fn=retry_quiz_ui,
inputs=[],
outputs=[question_md, feedback_md, progress_md, idx_state, score_state,
start_btn, btn_A, btn_B, btn_C, btn_D, retry_btn]
)
demo.launch()
|