Spaces:
Paused
Paused
Wenye He
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,97 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
-
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
generated_text = tokenizer.batch_decode(output_ids)[0]
|
| 23 |
-
# Extract assistant reply (assuming the template adds "<|assistant|>" marker)
|
| 24 |
-
answer = generated_text.split("<|assistant|>")[-1].strip()
|
| 25 |
-
chat_history.append({"role": "assistant", "content": answer})
|
| 26 |
-
return "", chat_history
|
| 27 |
-
|
| 28 |
-
with gr.Blocks() as phi_demo:
|
| 29 |
-
gr.Markdown("# Phi Chatbot")
|
| 30 |
-
chatbot = gr.Chatbot()
|
| 31 |
-
state = gr.State([])
|
| 32 |
-
txt = gr.Textbox(placeholder="Enter your message")
|
| 33 |
-
txt.submit(generate_response_phi, [txt, state], [txt, chatbot])
|
| 34 |
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 17 |
+
class ChatModel:
|
| 18 |
+
def __init__(self):
|
| 19 |
+
self.model = None
|
| 20 |
+
self.tokenizer = None
|
| 21 |
+
self.current_model = None
|
| 22 |
+
|
| 23 |
+
def load_model(self, model_name):
|
| 24 |
+
if model_name != self.current_model:
|
| 25 |
+
config = MODEL_CONFIG[model_name]
|
| 26 |
+
self.tokenizer = AutoTokenizer.from_pretrained(config["model_name"])
|
| 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):
|
| 66 |
+
response = model_handler.generate(message, model_choice, history)
|
| 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()
|