Spaces:
Runtime error
Runtime error
Download app.py from gampala1234/phi4mini: direct link, hf CLI and curl.
- Browser
- Download file 1.29 kB
-
https://huggingface.co/spaces/gampala1234/phi4mini/resolve/main/app.py
- Command line
-
hf download hf://spaces/gampala1234/phi4mini/app.py
-
curl -L -o app.py https://huggingface.co/spaces/gampala1234/phi4mini/resolve/main/app.py
1.29 kB
| import gradio as gr | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, BitsAndBytesConfig | |
| # Model name from Hugging Face Hub | |
| model_name = "microsoft/Phi-4-mini-instruct" | |
| # Configure 8-bit quantization for RAM efficiency | |
| bnb_config = BitsAndBytesConfig( | |
| load_in_8bit=True, | |
| bnb_8bit_use_double_quant=True, | |
| bnb_8bit_quant_type="nf4", | |
| ) | |
| # Load tokenizer and quantized model | |
| tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_name, | |
| quantization_config=bnb_config, | |
| device_map="auto", | |
| trust_remote_code=True | |
| ) | |
| # Set up text-generation pipeline | |
| generator = pipeline("text-generation", model=model, tokenizer=tokenizer) | |
| # Define chatbot response function | |
| def chat_maya(prompt): | |
| response = generator(prompt, max_new_tokens=300, do_sample=True, temperature=0.7)[0]["generated_text"] | |
| return response[len(prompt):] # Remove prompt echo | |
| # Create Gradio interface | |
| iface = gr.Interface( | |
| fn=chat_maya, | |
| inputs=gr.Textbox(lines=3, placeholder="Ask Maya anything..."), | |
| outputs="text", | |
| title="🧠 Maya AI - Student Assistant (Optimized)", | |
| description="Phi-3 Mini running in 8-bit for memory efficiency. Ask your questions below!" | |
| ) | |
| iface.launch() |