Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from google.colab import drive
|
| 6 |
+
|
| 7 |
+
# Install bitsandbytes and accelerate
|
| 8 |
+
!pip install bitsandbytes
|
| 9 |
+
!pip install accelerate
|
| 10 |
+
|
| 11 |
+
# Mount Google Drive
|
| 12 |
+
drive.mount('/content/drive')
|
| 13 |
+
|
| 14 |
+
# Set the path to the local directory where the model and tokenizer are saved
|
| 15 |
+
MODEL_PATH = "/content/drive/My Drive/phi35"
|
| 16 |
+
|
| 17 |
+
# Load the tokenizer from the local directory
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 19 |
+
|
| 20 |
+
# Load the model with 8-bit quantization
|
| 21 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 22 |
+
MODEL_PATH,
|
| 23 |
+
device_map='auto',
|
| 24 |
+
load_in_8bit=True
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Create the text-generation pipeline
|
| 28 |
+
pipe = pipeline(
|
| 29 |
+
"text-generation",
|
| 30 |
+
model=model,
|
| 31 |
+
tokenizer=tokenizer,
|
| 32 |
+
max_length=256, # Adjusted for faster inference
|
| 33 |
+
do_sample=True,
|
| 34 |
+
top_p=0.95,
|
| 35 |
+
top_k=50,
|
| 36 |
+
temperature=0.8,
|
| 37 |
+
device_map={'': 0}
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Define the function for the Gradio interface
|
| 41 |
+
def chat_with_phi(message):
|
| 42 |
+
response = pipe(message)
|
| 43 |
+
return response[0]['generated_text']
|
| 44 |
+
|
| 45 |
+
# Set up the Gradio interface
|
| 46 |
+
app = gr.Interface(
|
| 47 |
+
fn=chat_with_phi,
|
| 48 |
+
inputs=gr.Textbox(label="Type your message:"),
|
| 49 |
+
outputs=gr.Textbox(label="Phi 3.5 Responds:"),
|
| 50 |
+
title="Phi 3.5 Text Chat",
|
| 51 |
+
description="Chat with Phi 3.5 model. Ask anything!",
|
| 52 |
+
theme="default"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Launch the app
|
| 56 |
+
app.launch(debug=True)
|