vortex123's picture
Update app.py
74dc24d verified
raw
history blame
2.45 kB
import gradio as gr
import time
import re
MODELS = ["Mixtral-8x7B-Instruct-v0.1"]
def chat_with_ai(message, chat_history, system_prompt):
"""Formats the chat history for the API call."""
messages = [{"role": "system", "content": system_prompt}]
for item in chat_history:
messages.append({"role": "user", "content": item["user"]})
messages.append({"role": "assistant", "content": item.get("assistant", "")})
messages.append({"role": "user", "content": message})
return messages
def respond(message, chat_history, model, system_prompt, thinking_budget):
"""Simulate API call and get the response. Replace with actual API call."""
# Simulate a delay to mimic network or processing delay
time.sleep(thinking_budget / 10) # Simulated delay based on thinking budget
# Dummy response, replace this with an actual model call if necessary
response = f"Simulated response for: {message}"
return response, 1.0
def generate(message, history, model, system_prompt, thinking_budget):
"""Generates the chatbot response."""
chat_formatted = chat_with_ai(message, history, system_prompt)
response, thinking_time = respond(message, chat_formatted, model, system_prompt, thinking_budget)
history.append({"user": message, "assistant": response})
return history, ""
# Define the default system prompt
DEFAULT_SYSTEM_PROMPT = """
You are a helpful assistant in normal conversation.
When given a problem to solve, you are an expert problem-solving assistant.
Your task is to provide a detailed, step-by-step solution to a given question.
"""
with gr.Blocks() as demo:
gr.Markdown("# Custom Chat Interface")
with gr.Row():
model = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[0])
thinking_budget = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Thinking Budget")
system_prompt = gr.Textbox(value=DEFAULT_SYSTEM_PROMPT, lines=15, label="System Prompt")
chatbot = gr.Chatbot(label="Chat", type="messages")
msg = gr.Textbox(label="Type your message here...", placeholder="Enter your message...")
# Clear chat function
def clear_chat():
return [], ""
gr.Button("Clear Chat").click(clear_chat, inputs=None, outputs=[chatbot, msg])
# Generate response on message submission
msg.submit(generate, inputs=[msg, chatbot, model, system_prompt, thinking_budget], outputs=[chatbot, msg])
demo.launch()