CoolFace
Apppublic

Priya11/Interactive_Learning_Assistant

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
interactive_learning_assistant.py110 linesDownload Raw Back to root
1# -*- coding: utf-8 -*-2"""Interactive_learning_assistant.ipynb3 4Automatically generated by Colab.5 6Original file is located at7    https://colab.research.google.com/drive/1ZbWGkV5PKpCfwajzdQJcAgNLUzxIDzp98"""9 10# Install required libraries11!pip install transformers gradio torch12 13# Import libraries14import torch15from transformers import AutoTokenizer, AutoModelForCausalLM16import gradio as gr17 18# Model ID for DeepSeek-R119model_id = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"20 21# Load tokenizer and model22tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)23model = AutoModelForCausalLM.from_pretrained(24    model_id,25    torch_dtype=torch.float16,  # Use half-precision for faster inference26    device_map="auto",         # Automatically map model to GPU27    trust_remote_code=True28)29 30# Set the model to evaluation mode31model.eval()32print("DeepSeek-R1 loaded successfully!")33 34# Define the Interactive Learning Assistant35class LearningAssistant:36    def __init__(self):37        self.agent = model38        self.tokenizer = tokenizer39 40    def answer_question(self, question):41        # Create a prompt for the model42        prompt = f"You are a helpful learning assistant. Answer the following question in detail:\n\n{question}"43        inputs = self.tokenizer(prompt, return_tensors="pt").to(model.device)44 45        # Generate response46        outputs = self.agent.generate(47            inputs.input_ids,48            max_length=512,  # Limit response length49            temperature=0.7,  # Control creativity50            do_sample=True,51            top_p=0.952        )53 54        # Decode and return the response55        response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)56        return response57 58    def generate_quiz(self, topic):59        # Create a prompt for generating a quiz60        prompt = f"Generate a 5-question quiz on the topic of {topic}."61        inputs = self.tokenizer(prompt, return_tensors="pt").to(model.device)62 63        # Generate quiz64        outputs = self.agent.generate(65            inputs.input_ids,66            max_length=512,67            temperature=0.7,68            do_sample=True,69            top_p=0.970        )71 72        # Decode and return the quiz73        quiz = self.tokenizer.decode(outputs[0], skip_special_tokens=True)74        return quiz75 76# Create an instance of the LearningAssistant77assistant = LearningAssistant()78 79# Define a Gradio interface for the Learning Assistant80def interact_with_assistant(question, topic):81    # Answer the question82    answer = assistant.answer_question(question)83 84    # Generate a quiz on the topic85    quiz = assistant.generate_quiz(topic)86 87    # Return both the answer and the quiz88    return answer, quiz89 90# Gradio UI91with gr.Blocks() as demo:92    gr.Markdown("# Interactive Learning Assistant")93    with gr.Row():94        with gr.Column():95            question = gr.Textbox(label="Ask a Question", placeholder="Type your question here...")96            topic = gr.Textbox(label="Topic for Quiz", placeholder="Enter a topic to generate a quiz...")97            submit_btn = gr.Button("Submit")98        with gr.Column():99            answer = gr.Textbox(label="Answer", interactive=False)100            quiz = gr.Textbox(label="Generated Quiz", interactive=False)101 102    # Link the function to the button103    submit_btn.click(104        interact_with_assistant,105        inputs=[question, topic],106        outputs=[answer, quiz]107    )108 109# Launch the Gradio app110demo.launch(share=True)  # Set `share=True` to get a public link