Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,18 +1,106 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
premium_models = [
|
| 4 |
"HuggingFaceH4/zephyr-7b-beta",
|
| 5 |
-
|
| 6 |
"Qwen/Qwen2.5-Omni-7B",
|
| 7 |
"Qwen/Qwen2.5-VL-7B-Instruct",
|
| 8 |
"deepseek-ai/Janus-Pro-7B",
|
| 9 |
"meta-llama/Llama-2-7b-hf",
|
| 10 |
"Alibaba-NLP/gte-Qwen2-7B-instruct",
|
| 11 |
-
|
| 12 |
]
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''🔧 Prerequisites
|
| 2 |
+
|
| 3 |
+
Install the necessary packages:
|
| 4 |
+
|
| 5 |
+
pip install gradio transformers
|
| 6 |
+
|
| 7 |
+
📱 Gradio Chatbot App Code
|
| 8 |
+
'''
|
| 9 |
+
import gradio as gr
|
| 10 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 11 |
+
import torch
|
| 12 |
+
|
| 13 |
+
# List of available premium models
|
| 14 |
premium_models = [
|
| 15 |
"HuggingFaceH4/zephyr-7b-beta",
|
| 16 |
+
"K00B404/BagOClownCoders-slerp-7B",
|
| 17 |
"Qwen/Qwen2.5-Omni-7B",
|
| 18 |
"Qwen/Qwen2.5-VL-7B-Instruct",
|
| 19 |
"deepseek-ai/Janus-Pro-7B",
|
| 20 |
"meta-llama/Llama-2-7b-hf",
|
| 21 |
"Alibaba-NLP/gte-Qwen2-7B-instruct",
|
|
|
|
| 22 |
]
|
| 23 |
+
|
| 24 |
+
# Dictionary to cache loaded pipelines
|
| 25 |
+
pipeline_cache = {}
|
| 26 |
+
|
| 27 |
+
# Initial system prompt
|
| 28 |
+
default_system_prompt = "You are a ChatBuddy and chat with the user in a Human way."
|
| 29 |
+
|
| 30 |
+
def load_pipeline(model_name):
|
| 31 |
+
if model_name not in pipeline_cache:
|
| 32 |
+
print(f"Loading model: {model_name}")
|
| 33 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 34 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
| 35 |
+
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0 if torch.cuda.is_available() else -1)
|
| 36 |
+
pipeline_cache[model_name] = pipe
|
| 37 |
+
return pipeline_cache[model_name]
|
| 38 |
+
|
| 39 |
+
def chatbot(user_input, history, model_choice):
|
| 40 |
+
pipe = load_pipeline(model_choice)
|
| 41 |
+
|
| 42 |
+
# Prepare the chat messages
|
| 43 |
+
messages = [{"role": "system", "content": default_system_prompt}]
|
| 44 |
+
for pair in history:
|
| 45 |
+
messages.append({"role": "user", "content": pair[0]})
|
| 46 |
+
messages.append({"role": "assistant", "content": pair[1]})
|
| 47 |
+
messages.append({"role": "user", "content": user_input})
|
| 48 |
+
|
| 49 |
+
# Flatten into a prompt string
|
| 50 |
+
prompt = ""
|
| 51 |
+
for msg in messages:
|
| 52 |
+
if msg["role"] == "system":
|
| 53 |
+
prompt += f"<|system|> {msg['content']}\n"
|
| 54 |
+
elif msg["role"] == "user":
|
| 55 |
+
prompt += f"<|user|> {msg['content']}\n"
|
| 56 |
+
elif msg["role"] == "assistant":
|
| 57 |
+
prompt += f"<|assistant|> {msg['content']}\n"
|
| 58 |
+
|
| 59 |
+
# Generate a response
|
| 60 |
+
response = pipe(prompt, max_new_tokens=200, do_sample=True, top_p=0.95, temperature=0.7)[0]['generated_text']
|
| 61 |
+
|
| 62 |
+
# Extract only the last assistant response
|
| 63 |
+
split_res = response.split("<|assistant|>")
|
| 64 |
+
final_response = split_res[-1].strip() if len(split_res) > 1 else response
|
| 65 |
+
|
| 66 |
+
history.append((user_input, final_response))
|
| 67 |
+
return "", history
|
| 68 |
+
|
| 69 |
+
with gr.Blocks() as demo:
|
| 70 |
+
gr.Markdown("# 🤖 ChatBuddy - Advanced Chatbot with Selectable LLMs")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
model_choice = gr.Dropdown(label="Select Model", choices=premium_models, value=premium_models[0])
|
| 74 |
+
|
| 75 |
+
chatbot_ui = gr.Chatbot()
|
| 76 |
+
user_input = gr.Textbox(show_label=False, placeholder="Type your message and press Enter")
|
| 77 |
+
clear_btn = gr.Button("Clear")
|
| 78 |
+
|
| 79 |
+
state = gr.State([])
|
| 80 |
+
|
| 81 |
+
user_input.submit(chatbot, [user_input, state, model_choice], [user_input, chatbot_ui])
|
| 82 |
+
clear_btn.click(lambda: ([], ""), None, [chatbot_ui, state])
|
| 83 |
+
|
| 84 |
+
demo.launch()
|
| 85 |
+
'''
|
| 86 |
+
✅ Features:
|
| 87 |
+
|
| 88 |
+
Model selection from dropdown
|
| 89 |
+
|
| 90 |
+
Maintains chat history
|
| 91 |
+
|
| 92 |
+
Respects a system prompt
|
| 93 |
+
|
| 94 |
+
Uses text-generation pipeline
|
| 95 |
+
|
| 96 |
+
🧠 Optional Upgrades:
|
| 97 |
+
|
| 98 |
+
Replace text-generation with chat-completion if models support it (like OpenChat, Mistral-instruct, etc.)
|
| 99 |
+
|
| 100 |
+
Add streaming or token-by-token response if supported
|
| 101 |
+
|
| 102 |
+
Save/load chat history
|
| 103 |
+
|
| 104 |
+
Add support for vision models (Qwen2.5-VL-7B-Instruct) using a different UI tab
|
| 105 |
+
|
| 106 |
+
'''
|