File size: 1,405 Bytes
c9c01d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import gradio as gr
from google.colab import drive

# Install bitsandbytes and accelerate
!pip install bitsandbytes
!pip install accelerate

# Mount Google Drive
drive.mount('/content/drive')

# Set the path to the local directory where the model and tokenizer are saved
MODEL_PATH = "/content/drive/My Drive/phi35"

# Load the tokenizer from the local directory
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)

# Load the model with 8-bit quantization
model = AutoModelForCausalLM.from_pretrained(
    MODEL_PATH,
    device_map='auto',
    load_in_8bit=True
)

# Create the text-generation pipeline
pipe = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    max_length=256,  # Adjusted for faster inference
    do_sample=True,
    top_p=0.95,
    top_k=50,
    temperature=0.8,
    device_map={'': 0}
)

# Define the function for the Gradio interface
def chat_with_phi(message):
    response = pipe(message)
    return response[0]['generated_text']

# Set up the Gradio interface
app = gr.Interface(
    fn=chat_with_phi,
    inputs=gr.Textbox(label="Type your message:"),
    outputs=gr.Textbox(label="Phi 3.5 Responds:"),
    title="Phi 3.5 Text Chat",
    description="Chat with Phi 3.5 model. Ask anything!",
    theme="default"
)

# Launch the app
app.launch(debug=True)