File size: 3,585 Bytes
309f0f0 e580f8c b13d2bd ae01722 72c5353 1659a4a 72c5353 309f0f0 72c5353 309f0f0 72c5353 b13d2bd 9ec6ecf 1659a4a b5831f8 e693e08 72c5353 b13d2bd e693e08 b13d2bd f8bfa33 b13d2bd 72c5353 e693e08 b5831f8 b13d2bd b5831f8 b13d2bd b5831f8 72c5353 b13d2bd 309f0f0 b13d2bd 309f0f0 b13d2bd 309f0f0 b13d2bd f8bfa33 b13d2bd ae01722 b13d2bd f8bfa33 b13d2bd b5831f8 72c5353 b5831f8 1659a4a 309f0f0 72c5353 | 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 | import gradio as gr
import os
import json
from file_utils import extract_text_from_file, call_llm
def file_viewer_dashboard(nickname_input):
with gr.Column(scale=4, elem_id="main-column") as file_panel:
file_upload = gr.File(label="π Upload a File", file_types=[".pdf", ".txt", ".md", ".pptx"])
preview = gr.Textbox(label="π File Content Preview", lines=10, interactive=False)
file_text_state = gr.State("")
chat_history = gr.State([])
chat_output = gr.Chatbot()
chat_input = gr.Textbox(
placeholder="Ask about the file... (press Enter or click π€)",
label=None,
lines=1,
elem_id="chat-input",
)
send_btn = gr.Button("π€", size="sm", elem_id="chat-send-btn")
btn_simplify = gr.Button("π§Ύ Simplify Content")
simplified_box = gr.Textbox(label="π Simplified File Version", lines=15, interactive=False)
quiz_btn = gr.Button("π§ Generate Quiz")
quiz_score = gr.Textbox(label="Your Score", interactive=False, visible=False)
quiz_box = gr.Column(visible=False)
def load_and_preview(file):
if not file:
return "", ""
text = extract_text_from_file(file)
return text, text
def handle_chat(query, text, history):
prompt = f"Refer only to this file and answer:\n\n{text}\n\nQuestion: {query}"
answer = call_llm(prompt)
history.append((query, answer))
return history, ""
def simplify_content(text):
prompt = f"""
Reformat the following content to improve readability and clarity:
- Use simple language
- Shorter paragraphs
- Larger font layout (for display)
- Use clear sections or bullet points when needed
- Preserve all original meaning
Text:
{text}
"""
return call_llm(prompt)
def generate_quiz(text):
prompt = "Create 5 multiple-choice questions from this text. Return JSON list."
response = call_llm(prompt)
try:
questions = json.loads(response)
elements = []
for i, q in enumerate(questions):
q_md = gr.Markdown(f"**Q{i+1}. {q['question']}**")
q_radio = gr.Radio(q["options"], label="Select an answer")
q_feedback = gr.Textbox(label="Result", interactive=False)
def grade(choice, correct=q["answer"]):
return "β
Correct!" if choice == correct else f"β Correct: {correct}"
q_radio.change(fn=grade, inputs=[q_radio], outputs=[q_feedback])
elements.extend([q_md, q_radio, q_feedback])
return gr.update(visible=True), elements, gr.update(visible=True, value="Score: 0/5")
except:
return gr.update(visible=False), [], gr.update(visible=True, value="Error generating quiz.")
file_upload.change(fn=load_and_preview, inputs=[file_upload], outputs=[preview, file_text_state])
send_btn.click(fn=handle_chat, inputs=[chat_input, file_text_state, chat_history], outputs=[chat_output, chat_input])
chat_input.submit(fn=handle_chat, inputs=[chat_input, file_text_state, chat_history], outputs=[chat_output, chat_input])
btn_simplify.click(fn=simplify_content, inputs=[file_text_state], outputs=[simplified_box])
quiz_btn.click(fn=generate_quiz, inputs=[file_text_state], outputs=[quiz_box, quiz_box, quiz_score])
return file_panel |