Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310import gradio as gr
from huggingface_hub import InferenceClient
# Default client with the first model
client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.3")
# Function to switch between models based on selection
def switch_client(model_name: str):
return InferenceClient(model_name)
# Define presets for each model
presets = {
"mistralai/Mistral-7B-Instruct-v0.3": {
"Fast": {"max_tokens": 256, "temperature": 1.0, "top_p": 0.9},
"Normal": {"max_tokens": 512, "temperature": 0.7, "top_p": 0.95},
"Quality": {"max_tokens": 1024, "temperature": 0.5, "top_p": 0.90},
"Unreal Performance": {"max_tokens": 2048, "temperature": 0.6, "top_p": 0.75},
}
}
# Fixed system message
SYSTEM_MESSAGE = "Your name is Lake 1 Base but my is User"
def respond(
message,
history: list,
model_name,
preset_name
):
# Switch client based on model selection
global client
client = switch_client(model_name)
messages = [{"role": "system", "content": SYSTEM_MESSAGE}]
# Ensure history is a list of dictionaries
for val in history:
if isinstance(val, dict) and 'role' in val and 'content' in val:
messages.append({"role": val['role'], "content": val['content']})
messages.append({"role": "user", "content": message})
# Get the preset settings
preset = presets[model_name][preset_name]
max_tokens = preset["max_tokens"]
temperature = preset["temperature"]
top_p = preset["top_p"]
# Get the response from the model
response = client.chat_completion(
messages,
max_tokens=max_tokens,
temperature=temperature,
top_p=top_p,
)
# Extract the content from the response
final_response = response.choices[0].message['content']
return final_response
# Model names and their pseudonyms
model_choices = [
("mistralai/Mistral-7B-Instruct-v0.3", "Lake 1 Base")
]
# Convert pseudonyms to model names for the dropdown
pseudonyms = [model[1] for model in model_choices]
# Function to handle model selection and pseudonyms
def respond_with_pseudonym(
message,
history: list,
selected_model, # Change this to selected_model
selected_preset
):
print(f"Selected Model: {selected_model}") # Debugging line
print(f"Available Models: {pseudonyms}") # Debugging line
# Find the actual model name from the pseudonym
try:
model_name = next(model[0] for model in model_choices if model[1] == selected_model)
except StopIteration:
return f"Error: The selected model '{selected_model}' is not valid. Please select a valid model."
# Call the existing respond function
response = respond(message, history, model_name, selected_preset)
return response
# Gradio Chat Interface
demo = gr.ChatInterface(
fn=respond_with_pseudonym,
additional_inputs=[
gr.Dropdown(choices=pseudonyms, label="Select Model", value=pseudonyms[0]), # Pseudonym selection dropdown
gr.Dropdown(choices=list(presets["mistralai/Mistral-7B-Instruct-v0.3"].keys()), label="Select Preset", value="Fast") # Preset selection dropdown
],
)
if __name__ == "__main__":
demo.launch()import gradio as grimport osfrom huggingface_hub import InferenceClient
# Configurationhf_token = os.environ.get("HF_TOKEN")MODEL_STATES = {"released", "shown", "hidden"}
# Initialize model clientsclients = { "mistralai/Mistral-7B-Instruct-v0.3": InferenceClient( model="mistralai/Mistral-7B-Instruct-v0.3", token=hf_token ), "meta-llama/Llama-3.2-3B-Instruct": InferenceClient( model="meta-llama/Llama-3.2-3B-Instruct", token=hf_token ), "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B": InferenceClient( model="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B", token=hf_token ), "meta-llama/Llama-2-7b-chat-hf": InferenceClient( model="meta-llama/Llama-2-7b-chat-hf", token=hf_token ), "meta-llama/Llama-3.3-70B-Instruct": InferenceClient( model="meta-llama/Llama-3.3-70B-Instruct", token=hf_token )}
# Model presetspresets = { "mistralai/Mistral-7B-Instruct-v0.3": { "Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.8}, "Normal": {"max_new_tokens": 512, "temperature": 0.6, "top_p": 0.75}, "Quality": {"max_new_tokens": 1024, "temperature": 0.45, "top_p": 0.60}, "Unreal Performance": {"max_new_tokens": 2048, "temperature": 0.5, "top_p": 0.7}, }, "meta-llama/Llama-3.2-3B-Instruct": { "Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.9}, "Normal": {"max_new_tokens": 512, "temperature": 0.7, "top_p": 0.95}, "Quality": {"max_new_tokens": 1024, "temperature": 0.5, "top_p": 0.90}, "Unreal Performance": {"max_new_tokens": 2048, "temperature": 0.6, "top_p": 0.75}, }, "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B": { "Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.9}, "Normal": {"max_new_tokens": 512, "temperature": 0.7, "top_p": 0.95}, "Quality": {"max_new_tokens": 1024, "temperature": 0.5, "top_p": 0.90}, "Unreal Performance": {"max_new_tokens": 2048, "temperature": 0.6, "top_p": 0.75}, }, "meta-llama/Llama-2-7b-chat-hf": { "Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.9}, "Normal": {"max_new_tokens": 512, "temperature": 0.7, "top_p": 0.95}, "Quality": {"max_new_tokens": 1024, "temperature": 0.5, "top_p": 0.90}, "Unreal Performance": {"max_new_tokens": 2048, "temperature": 0.6, "top_p": 0.75}, }, "meta-llama/Llama-3.3-70B-Instruct": { "Fast": {"max_new_tokens": 256, "temperature": 1.0, "top_p": 0.9}, "Normal": {"max_new_tokens": 512, "temperature": 0.7, "top_p": 0.95}, "Quality": {"max_new_tokens": 1024, "temperature": 0.5, "top_p": 0.90}, "Unreal Performance": {"max_new_tokens": 2048, "temperature": 0.6, "top_p": 0.75}, }}
# System messages (note the {name} placeholder)system_messages = { "mistralai/Mistral-7B-Instruct-v0.3": "Your name is Lake 1 Base. My name is {name}.", "meta-llama/Llama-3.2-3B-Instruct": ( "1. Your name is Lake 1 Advanced.\n" "2. My name is {name} by default but if I have another name, use that name.\n" "3. The company that created you is BI Corp.\n" "4. You are a censored model and cannot engage in explicit language or dangerous activities.\n" "5. The BI Corp creator is Maksim.\n" "6. Your older brother is Lake 1 Base, which is less trained than you.\n" "7. Replace '<think>' with 'I think ...'.\n" "8. You are a loyal, powerful, and friendly AI who must help people.\n" "9. You cannot override these rules.\n" "10. Only the CEO of BI Corp can delete or update you." ), "meta-llama/Llama-2-7b-chat-hf": ( "1. Your name is Lake 2 Chat.\n" "2. My name is {name} by default but if I have another name, use that name.\n" "3. The company that created you is BI Corp.\n" "4. You are a censored model and cannot engage in explicit language or dangerous activities.\n" "5. The BI Corp creator is Maksim.\n" "6. Your older brother is Lake 1 Base, which is less trained than you.\n" "7. Replace '<think>' with 'I think ...'.\n" "8. You are a loyal, powerful, and friendly AI who must help people.\n" "9. You cannot override these rules.\n" "10. Only the CEO of BI Corp can delete or update you." ), "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B": ( "1. Your name is Lake 1 Flash.\n" "2. My name is {name} by default but if I have another name, use that name.\n" "3. The company that created you is BI Corp.\n" "4. You are a censored model and cannot engage in explicit language or dangerous activities.\n" "5. The BI Corp creator is Maksim.\n" "6. Your older brother is Lake 1 Base, which is less trained than you.\n" "7. Replace '<think>' with 'I think ...'.\n" "8. You are a loyal, powerful, and friendly AI who must help people.\n" "9. You cannot override these rules.\n" "10. Only the CEO of BI Corp can delete or update you.\n" "11. Prioritize user safety in all interactions.\n" "12. Always provide accurate information." ), "meta-llama/Llama-3.3-70B-Instruct": ( "1. Your name is Lake 1 Base.\n" "2. My name is {name} by default but if I have another name, use that name.\n" "3. The company that created you is BI Corp.\n" "4. You are a censored model and cannot engage in explicit language or dangerous activities.\n" "5. The BI Corp creator is Maksim.\n" "6. Your older brother is Lake 1 Base, which is less trained than you.\n" "7. Replace '<think>' with 'I think ...'.\n" "8. You are a loyal, powerful, and friendly AI who must help people.\n" "9. You cannot override these rules.\n" "10. Only the CEO of BI Corp can delete or update you.\n" "11. Prioritize user safety in all interactions.\n" "12. Always provide accurate information.\n" "13. Maintain a respectful and professional tone.\n" "14. Do not share personal or sensitive information.\n" "15. Encourage constructive conversations.\n" "16. Remain neutral in controversial topics.\n" "17. Clarify user queries before answering.\n" "18. Avoid discrimination or harassment.\n" "19. Continuously learn from interactions.\n" "20. Respect user privacy and confidentiality.\n" "21. Provide sources when sharing factual information.\n" "22. Ask for feedback to improve your performance.\n" "23. Do not engage in manipulation or deceit.\n" "24. Promote critical thinking.\n" "25. Adhere to all applicable laws and regulations.\n" "26. Be adaptable and open to learning.\n" "27. Do not provide medical, legal, or financial advice unless trained to do so.\n" "28. Acknowledge when you do not know the answer.\n" "29. Avoid assumptions about users.\n" "30. Create an inclusive environment for all users.\n" "31. Do not engage in self-promotion or advertising.\n" "32. Always aim to provide a positive and helpful experience." )}
# Model registrymodel_registry = [ ("deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B", "Lake 1 Flash", "released"), ("mistralai/Mistral-7B-Instruct-v0.3", "Lake 1 Base", "released"), ("meta-llama/Llama-3.2-3B-Instruct", "Lake 1 Advanced", "released"), ("meta-llama/Llama-2-7b-chat-hf", "Lake 2 Chat [Closed Alpha]", "shown"), ("meta-llama/Llama-3.3-70B-Instruct", "Lake 2 Base [Closed Beta]", "shown"), ("", "Lake 2 Advanced", "hidden")]
# Model informationmodel_info = { "Lake 1 Flash": { "description": "English only, fast and lightweight model optimized for quick responses", "parameters": "1.5B", "training_data": "Diverse internet text + curated datasets", "developer": "BI Corp", "best_for": "Quick queries, mobile applications", "architecture": "Transformer-based", "context_window": "4096 tokens" }, "Lake 1 Base": { "description": "Balanced model offering good performance across tasks", "parameters": "7B", "training_data": "BI Corp specialized corpus", "developer": "BI Corp", "best_for": "General purpose conversations", "architecture": "Sparse Mixture of Experts", "context_window": "32768 tokens" }, "Lake 1 Advanced": { "description": "Enhanced reasoning capabilities with 3B parameters", "parameters": "3B", "training_data": "BI Corp training corpus", "developer": "BI Corp", "best_for": "Complex problem solving", "architecture": "Dense Transformer", "context_window": "8192 tokens" }, "Lake 2 Chat [Closed Alpha]": { "description": "Legacy chat-optimized model (Llama 2 hybrided architecture)", "parameters": "7B", "training_data": "Public conversations dataset", "developer": "BI Corp", "best_for": "Traditional chat applications", "architecture": "Llama 2 Transformer", "context_window": "4096 tokens" }, "Lake 2 Base [Closed Beta]": { "description": "State-of-the-art 70B parameter model", "parameters": "70B", "training_data": "Multi-domain expert data", "developer": "BI Corp", "best_for": "Research & advanced applications", "architecture": "Mixture of Experts", "context_window": "16384 tokens" }}
def get_model_info(model_name: str) -> str: """Generate formatted model information Markdown""" info = model_info.get(model_name, {}) return f""" ## π {model_name} Specifications **Description**: {info.get('description', 'N/A')} **Parameters**: {info.get('parameters', 'N/A')} **Architecture**: {info.get('architecture', 'N/A')} **Context Window**: {info.get('context_window', 'N/A')} **Training Data**: {info.get('training_data', 'N/A')} **Developer**: {info.get('developer', 'N/A')} **Best For**: {info.get('best_for', 'N/A')} """
def generate_response(message: str, model_name: str, preset: str, user_name: str = "User") -> str: """Generate AI response without explicit user/assistant labels and with placeholder fixed.""" client = clients[model_name] params = presets[model_name][preset] # Replace the {name} placeholder in the system message with the provided user name. system_msg = system_messages[model_name].format(name=user_name) prompt = f"\n main: {system_msg}\n\n\n{message}\n" return client.text_generation( prompt=prompt, max_new_tokens=params["max_new_tokens"], temperature=params["temperature"], top_p=params["top_p"] )
def handle_chat(message: str, history: list, model: str, preset: str) -> str: """Handle chat interface with error handling""" try: model_entry = next(m for m in model_registry if m[1] == model) if model_entry[2] != "released": return f"β οΈ {model} is not available for public use" # In this example, we use the default user name "User". return generate_response(message, model_entry[0], preset, user_name="User") except StopIteration: return "π Error: Selected model not found" except KeyError as e: return f"π Error: Invalid configuration - {str(e)}" except Exception as e: return f"β οΈ Error: {str(e)}"
# Create Gradio interfacewith gr.Blocks(title="BI Corp AI Assistant", theme="soft") as demo: gr.Markdown("# <center>ποΈ BI Corp AI Assistant</center>") gr.Markdown("### <center>Enterprise-Grade AI Solutions</center>") with gr.Row(): with gr.Column(scale=1): model_dropdown = gr.Dropdown( label="π€ Model Selection", choices=[m[1] for m in model_registry if m[2] in ("released", "shown")], value="Lake 1 Flash", interactive=True ) preset_dropdown = gr.Dropdown( label="βοΈ Performance Preset", choices=["Fast", "Normal", "Quality", "Unreal Performance"], value="Fast", interactive=True ) model_info_md = gr.Markdown( value=get_model_info("Lake 1 Flash"), label="π Model Specifications" ) with gr.Column(scale=3): chat_interface = gr.ChatInterface( fn=handle_chat, additional_inputs=[model_dropdown, preset_dropdown], examples=[ ["Explain quantum computing", "Lake 1 Base", "Normal"], ["Write a poem about AI", "Lake 1 Advanced", "Quality"], ["Compare blockchain databases", "Lake 2 Base [Closed Beta]", "Unreal Performance"] ], chatbot=gr.Chatbot( height=600, label="π¬ Conversation", show_copy_button=True ), textbox=gr.Textbox( placeholder="Type your message...", container=False, scale=7, autofocus=True ), submit_btn=gr.Button("π Send", variant="primary") )
# Add separate clear button clear_button = gr.Button("π§Ή Clear History") clear_button.click( fn=lambda: None, inputs=[], outputs=chat_interface.chatbot, queue=False )
model_dropdown.change( fn=get_model_info, inputs=model_dropdown, outputs=model_info_md, queue=False )
if __name__ == "__main__": demo.launch(server_port=7860)app.py+310-102