Spaces:
Runtime error
Runtime error
Download app.py from jitxz/class10thz: direct link, hf CLI and curl.
- Browser
- Download file 4.17 kB
-
https://huggingface.co/spaces/jitxz/class10thz/resolve/main/app.py
- Command line
-
hf download hf://spaces/jitxz/class10thz/app.py
-
curl -L -o app.py https://huggingface.co/spaces/jitxz/class10thz/resolve/main/app.py
4.17 kB
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| import torch | |
| import re | |
| # FREE and BEST model for Class 10 students | |
| MODEL_NAME = "meta-llama/Llama-3.1-8B-Instruct" | |
| print("Loading AI model... This may take 30-60 seconds on first run") | |
| # Load tokenizer and model | |
| tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| MODEL_NAME, | |
| torch_dtype=torch.float16, | |
| device_map="auto", | |
| load_in_8bit=True # Makes it faster and use less memory | |
| ) | |
| # Add padding token if not present | |
| if tokenizer.pad_token is None: | |
| tokenizer.pad_token = tokenizer.eos_token | |
| def format_class10_response(text): | |
| """Clean and format the AI response for Class 10 students""" | |
| # Remove repetitive content | |
| text = re.sub(r'(Note:|Important:|Remember:).*', '', text, flags=re.IGNORECASE) | |
| # Remove extra whitespace | |
| text = re.sub(r'\n{3,}', '\n\n', text) | |
| text = text.strip() | |
| return text | |
| def generate_response(message, history, system_message, max_tokens, temperature): | |
| """ | |
| Generate AI response optimized for Class 10 students | |
| """ | |
| try: | |
| # Build conversation history | |
| messages = [{"role": "system", "content": system_message}] | |
| # Add conversation history | |
| for human, assistant in history: | |
| messages.append({"role": "user", "content": human}) | |
| messages.append({"role": "assistant", "content": assistant}) | |
| # Add current message | |
| messages.append({"role": "user", "content": message}) | |
| # Format the input | |
| input_text = tokenizer.apply_chat_template(messages, tokenize=False) | |
| # Tokenize input | |
| inputs = tokenizer(input_text, return_tensors="pt", return_attention_mask=False) | |
| # Generate response | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| input_ids=inputs["input_ids"], | |
| max_new_tokens=max_tokens, | |
| temperature=temperature, | |
| do_sample=True, | |
| pad_token_id=tokenizer.eos_token_id, | |
| top_p=0.95, | |
| repetition_penalty=1.1 | |
| ) | |
| # Decode response | |
| response = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| # Extract only the assistant's response | |
| if "assistant" in response: | |
| response = response.split("assistant")[-1].strip() | |
| # Clean and format | |
| response = format_class10_response(response) | |
| # Ensure response isn't empty | |
| if not response or len(response) < 10: | |
| response = "I understand your question! Let me help you with that..." | |
| return response | |
| except Exception as e: | |
| return f"Sorry, I'm having trouble thinking right now. Please try asking your question again! 🤔" | |
| # Create the Gradio interface | |
| demo = gr.ChatInterface( | |
| fn=generate_response, | |
| title="🎓 Class 10 AI Assistant", | |
| description="""Your **FREE** AI study buddy powered by Llama 3.1! | |
| Ask me anything about Math, Science, English, History, or Computer Science!""", | |
| examples=[ | |
| "Explain the Pythagorean theorem in simple words", | |
| "What are the main types of chemical reactions?", | |
| "How do I write a good essay introduction?", | |
| "What is photosynthesis and why is it important?", | |
| "Explain Newton's three laws of motion" | |
| ], | |
| additional_inputs=[ | |
| gr.Textbox( | |
| value="""You are a helpful AI assistant for Class 10 students. | |
| Explain concepts clearly and simply. Be encouraging and patient. | |
| Use examples that students can relate to.""", | |
| label="System Message", | |
| visible=False | |
| ), | |
| gr.Slider(minimum=50, maximum=500, value=200, step=10, label="Max Response Length"), | |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Creativity Level"), | |
| ], | |
| theme="soft", | |
| submit_btn="Ask AI", | |
| retry_btn="Try Again", | |
| undo_btn="Undo", | |
| clear_btn="Clear Chat", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |