Spaces:
Paused
Paused
Wenye He
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
import torch
|
|
|
|
| 4 |
|
|
|
|
| 5 |
MODEL_CONFIG = {
|
| 6 |
-
"
|
| 7 |
-
"model_name": "
|
| 8 |
-
"template": "
|
| 9 |
},
|
| 10 |
-
"phi
|
| 11 |
-
"model_name": "microsoft/phi-
|
| 12 |
-
"template": "
|
| 13 |
}
|
| 14 |
}
|
| 15 |
|
|
@@ -26,32 +27,39 @@ class ChatModel:
|
|
| 26 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
config["model_name"],
|
| 28 |
torch_dtype=torch.float16,
|
| 29 |
-
device_map="auto"
|
| 30 |
-
load_in_4bit=True # Quantization to reduce memory usage
|
| 31 |
)
|
| 32 |
self.current_model = model_name
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
def generate(self, message, model_name, history):
|
| 35 |
self.load_model(model_name)
|
| 36 |
-
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
prompt = config["template"].format(message=message)
|
| 40 |
-
|
| 41 |
-
# Create pipeline
|
| 42 |
pipe = pipeline(
|
| 43 |
"text-generation",
|
| 44 |
model=self.model,
|
| 45 |
tokenizer=self.tokenizer,
|
| 46 |
-
max_new_tokens=150,
|
| 47 |
-
temperature=0.3,
|
| 48 |
-
do_sample=True,
|
| 49 |
device_map="auto"
|
| 50 |
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
| 55 |
model_handler = ChatModel()
|
| 56 |
|
| 57 |
def chat(message, history, model_choice):
|
|
@@ -59,19 +67,31 @@ def chat(message, history, model_choice):
|
|
| 59 |
return [(message, response)]
|
| 60 |
|
| 61 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 62 |
-
gr.Markdown("#
|
|
|
|
| 63 |
with gr.Row():
|
| 64 |
model_choice = gr.Dropdown(
|
| 65 |
-
choices=["
|
| 66 |
label="Select Model",
|
| 67 |
-
value="
|
| 68 |
)
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
msg.submit(
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 4 |
|
| 5 |
+
# Model configurations
|
| 6 |
MODEL_CONFIG = {
|
| 7 |
+
"llama": {
|
| 8 |
+
"model_name": "meta-llama/Llama-2-7b-chat-hf",
|
| 9 |
+
"template": "[INST] {message} [/INST]"
|
| 10 |
},
|
| 11 |
+
"phi": {
|
| 12 |
+
"model_name": "microsoft/phi-2",
|
| 13 |
+
"template": "{message}"
|
| 14 |
}
|
| 15 |
}
|
| 16 |
|
|
|
|
| 27 |
self.model = AutoModelForCausalLM.from_pretrained(
|
| 28 |
config["model_name"],
|
| 29 |
torch_dtype=torch.float16,
|
| 30 |
+
device_map="auto"
|
|
|
|
| 31 |
)
|
| 32 |
self.current_model = model_name
|
| 33 |
|
| 34 |
+
def format_message(self, message, model_name):
|
| 35 |
+
return MODEL_CONFIG[model_name]["template"].format(message=message)
|
| 36 |
+
|
| 37 |
def generate(self, message, model_name, history):
|
| 38 |
self.load_model(model_name)
|
| 39 |
+
formatted_message = self.format_message(message, model_name)
|
| 40 |
|
| 41 |
+
# Create pipeline for text generation
|
|
|
|
|
|
|
|
|
|
| 42 |
pipe = pipeline(
|
| 43 |
"text-generation",
|
| 44 |
model=self.model,
|
| 45 |
tokenizer=self.tokenizer,
|
|
|
|
|
|
|
|
|
|
| 46 |
device_map="auto"
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# Generate response
|
| 50 |
+
response = pipe(
|
| 51 |
+
formatted_message,
|
| 52 |
+
max_length=200,
|
| 53 |
+
do_sample=True,
|
| 54 |
+
temperature=0.7,
|
| 55 |
+
top_k=50,
|
| 56 |
+
top_p=0.95,
|
| 57 |
+
pad_token_id=self.tokenizer.eos_token_id
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
return response[0]['generated_text'].replace(formatted_message, "").strip()
|
| 61 |
|
| 62 |
+
# Initialize model handler
|
| 63 |
model_handler = ChatModel()
|
| 64 |
|
| 65 |
def chat(message, history, model_choice):
|
|
|
|
| 67 |
return [(message, response)]
|
| 68 |
|
| 69 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 70 |
+
gr.Markdown("# 🤖 Local LLM Chatbot\nSelect a model and start chatting!")
|
| 71 |
+
|
| 72 |
with gr.Row():
|
| 73 |
model_choice = gr.Dropdown(
|
| 74 |
+
choices=["llama", "phi"],
|
| 75 |
label="Select Model",
|
| 76 |
+
value="phi"
|
| 77 |
)
|
| 78 |
+
|
| 79 |
+
chatbot = gr.Chatbot(height=400)
|
| 80 |
+
msg = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
| 81 |
+
|
| 82 |
+
with gr.Row():
|
| 83 |
+
submit_btn = gr.Button("Send")
|
| 84 |
+
clear_btn = gr.ClearButton([msg, chatbot])
|
| 85 |
|
| 86 |
+
msg.submit(
|
| 87 |
+
fn=chat,
|
| 88 |
+
inputs=[msg, chatbot, model_choice],
|
| 89 |
+
outputs=[chatbot]
|
| 90 |
+
)
|
| 91 |
+
submit_btn.click(
|
| 92 |
+
fn=chat,
|
| 93 |
+
inputs=[msg, chatbot, model_choice],
|
| 94 |
+
outputs=[chatbot]
|
| 95 |
+
)
|
| 96 |
|
| 97 |
demo.launch()
|