| 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 |