File size: 1,291 Bytes
49e1eb3
4475267
49e1eb3
0c57681
49e1eb3
ef0cda1
280e5f2
49e1eb3
 
 
 
 
 
54fd04b
49e1eb3
 
 
 
 
 
 
 
0c57681
49e1eb3
0c57681
 
49e1eb3
0c57681
 
49e1eb3
0c57681
49e1eb3
0c57681
 
 
 
49e1eb3
 
4475267
 
49e1eb3
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

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