jpruzcuen commited on
Commit ·
f95926d
1
Parent(s): 6e8eb29
retro fix
Browse files
app.py
CHANGED
|
@@ -1,87 +1,104 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from quiz import start_quiz, answer_question,
|
|
|
|
| 3 |
from llama_cpp import Llama
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
from functools import partial
|
| 6 |
|
| 7 |
-
# Load model
|
| 8 |
model_path = hf_hub_download(
|
| 9 |
repo_id="ebbalg/llama-finetome",
|
| 10 |
filename="llama-3.2-1b-instruct.Q4_K_M.gguf"
|
| 11 |
)
|
| 12 |
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=2, verbose=False, chat_format="llama-3")
|
| 13 |
|
| 14 |
-
# Generate and parse quiz
|
| 15 |
-
raw_quiz = create_quiz(llm)
|
| 16 |
-
parsed_quiz = parse_quiz(raw_quiz)
|
| 17 |
-
|
| 18 |
with gr.Blocks(title="TAI: AI Teacher Assistant") as demo:
|
| 19 |
-
gr.Markdown("
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
feedback_md = gr.Markdown("")
|
| 24 |
-
progress_md = gr.Markdown("")
|
| 25 |
|
| 26 |
-
# Answer buttons
|
| 27 |
with gr.Row():
|
| 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 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from quiz import start_quiz, answer_question, parsed_quiz, format_question
|
| 3 |
+
from chatbot import chat
|
| 4 |
from llama_cpp import Llama
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
from functools import partial
|
| 7 |
|
| 8 |
+
# Load model (unchanged)
|
| 9 |
model_path = hf_hub_download(
|
| 10 |
repo_id="ebbalg/llama-finetome",
|
| 11 |
filename="llama-3.2-1b-instruct.Q4_K_M.gguf"
|
| 12 |
)
|
| 13 |
llm = Llama(model_path=model_path, n_ctx=2048, n_threads=2, verbose=False, chat_format="llama-3")
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
with gr.Blocks(title="TAI: AI Teacher Assistant") as demo:
|
| 16 |
+
gr.Markdown("""
|
| 17 |
+
# TAI: Your AI Teacher Assistant
|
| 18 |
+
Ask questions about AI and Machine Learning! Test your understanding with a quiz.
|
| 19 |
+
""")
|
|
|
|
|
|
|
| 20 |
|
|
|
|
| 21 |
with gr.Row():
|
| 22 |
+
# Chat column
|
| 23 |
+
with gr.Column(scale=1):
|
| 24 |
+
gr.Markdown("## Ask me anything")
|
| 25 |
+
gr.ChatInterface(
|
| 26 |
+
fn=lambda message, history: chat(llm, message, history),
|
| 27 |
+
examples=[
|
| 28 |
+
"Which tasks can recurrent neural networks address?",
|
| 29 |
+
"Explain backpropagation in simple terms.",
|
| 30 |
+
"What are the benefits of regularization during training?",
|
| 31 |
+
"Write a short summary of advancements that allowed for deep neural networks to work."
|
| 32 |
+
]
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Quiz column
|
| 36 |
+
with gr.Column(scale=2):
|
| 37 |
+
gr.Markdown("## Test Yourself")
|
| 38 |
+
start_btn = gr.Button("Start Quiz", variant="primary")
|
| 39 |
+
question_md = gr.Markdown("")
|
| 40 |
+
feedback_md = gr.Markdown("")
|
| 41 |
+
progress_md = gr.Markdown("")
|
| 42 |
+
|
| 43 |
+
# Answer buttons
|
| 44 |
+
with gr.Row():
|
| 45 |
+
btn_A = gr.Button("A", visible=False)
|
| 46 |
+
btn_B = gr.Button("B", visible=False)
|
| 47 |
+
btn_C = gr.Button("C", visible=False)
|
| 48 |
+
btn_D = gr.Button("D", visible=False)
|
| 49 |
+
retry_btn = gr.Button("Retry", visible=False)
|
| 50 |
|
| 51 |
+
# States
|
| 52 |
+
idx_state = gr.State(0)
|
| 53 |
+
score_state = gr.State(0)
|
| 54 |
|
| 55 |
+
# Start quiz
|
| 56 |
+
def start_quiz_ui():
|
| 57 |
+
q_text, feedback, progress, idx, score = start_quiz(parsed_quiz)
|
| 58 |
+
return (
|
| 59 |
+
q_text, feedback, progress, idx, score,
|
| 60 |
+
gr.update(visible=False), # hide start
|
| 61 |
+
gr.update(visible=True), # show A
|
| 62 |
+
gr.update(visible=True), # show B
|
| 63 |
+
gr.update(visible=True), # show C
|
| 64 |
+
gr.update(visible=True), # show D
|
| 65 |
+
gr.update(visible=True) # show retry
|
| 66 |
+
)
|
| 67 |
|
| 68 |
+
start_btn.click(
|
| 69 |
+
fn=start_quiz_ui,
|
| 70 |
+
inputs=[],
|
| 71 |
+
outputs=[question_md, feedback_md, progress_md, idx_state, score_state,
|
| 72 |
+
start_btn, btn_A, btn_B, btn_C, btn_D, retry_btn]
|
| 73 |
+
)
|
| 74 |
|
| 75 |
+
# Answer button clicks
|
| 76 |
+
for letter, btn in zip(["A", "B", "C", "D"], [btn_A, btn_B, btn_C, btn_D]):
|
| 77 |
+
btn_fn = partial(answer_question, parsed_quiz, letter)
|
| 78 |
+
btn.click(
|
| 79 |
+
fn=btn_fn,
|
| 80 |
+
inputs=[idx_state, score_state],
|
| 81 |
+
outputs=[question_md, idx_state, score_state, feedback_md, progress_md]
|
| 82 |
+
)
|
| 83 |
|
| 84 |
+
# Retry button
|
| 85 |
+
def retry_quiz_ui():
|
| 86 |
+
q_text, feedback, progress, idx, score = start_quiz(parsed_quiz)
|
| 87 |
+
return (
|
| 88 |
+
q_text, feedback, progress, idx, score,
|
| 89 |
+
gr.update(visible=True), # show start
|
| 90 |
+
gr.update(visible=False), # hide A
|
| 91 |
+
gr.update(visible=False), # hide B
|
| 92 |
+
gr.update(visible=False), # hide C
|
| 93 |
+
gr.update(visible=False), # hide D
|
| 94 |
+
gr.update(visible=False) # hide retry
|
| 95 |
+
)
|
| 96 |
|
| 97 |
+
retry_btn.click(
|
| 98 |
+
fn=retry_quiz_ui,
|
| 99 |
+
inputs=[],
|
| 100 |
+
outputs=[question_md, feedback_md, progress_md, idx_state, score_state,
|
| 101 |
+
start_btn, btn_A, btn_B, btn_C, btn_D, retry_btn]
|
| 102 |
+
)
|
| 103 |
|
| 104 |
demo.launch()
|