Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,59 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
interface = gr.load("models/mistralai/Mixtral-8x7B-Instruct-v0.1")
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
DEFAULT_SYSTEM_PROMPT = """
|
8 |
You are a helpful assistant in normal conversation.
|
9 |
When given a problem to solve, you are an expert problem-solving assistant.
|
10 |
Your task is to provide a detailed, step-by-step solution to a given question.
|
11 |
"""
|
12 |
|
13 |
-
# Определение функции для очистки чата
|
14 |
-
def clear_chat():
|
15 |
-
return [], ""
|
16 |
-
|
17 |
-
# Создание интерфейса с пользовательскими элементами
|
18 |
with gr.Blocks() as demo:
|
19 |
gr.Markdown("# Custom Chat Interface")
|
20 |
-
|
21 |
with gr.Row():
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
26 |
msg = gr.Textbox(label="Type your message here...", placeholder="Enter your message...")
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
29 |
gr.Button("Clear Chat").click(clear_chat, inputs=None, outputs=[chatbot, msg])
|
30 |
|
|
|
|
|
|
|
31 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import time
|
3 |
+
import re
|
4 |
|
5 |
+
MODELS = ["Mixtral-8x7B-Instruct-v0.1"]
|
|
|
6 |
|
7 |
+
def chat_with_ai(message, chat_history, system_prompt):
|
8 |
+
"""Formats the chat history for the API call."""
|
9 |
+
messages = [{"role": "system", "content": system_prompt}]
|
10 |
+
for item in chat_history:
|
11 |
+
messages.append({"role": "user", "content": item["user"]})
|
12 |
+
messages.append({"role": "assistant", "content": item.get("assistant", "")})
|
13 |
+
|
14 |
+
messages.append({"role": "user", "content": message})
|
15 |
+
return messages
|
16 |
+
|
17 |
+
def respond(message, chat_history, model, system_prompt, thinking_budget):
|
18 |
+
"""Simulate API call and get the response. Replace with actual API call."""
|
19 |
+
# Simulate a delay to mimic network or processing delay
|
20 |
+
time.sleep(thinking_budget / 10) # Simulated delay based on thinking budget
|
21 |
+
# Dummy response, replace this with an actual model call if necessary
|
22 |
+
response = f"Simulated response for: {message}"
|
23 |
+
return response, 1.0
|
24 |
+
|
25 |
+
def generate(message, history, model, system_prompt, thinking_budget):
|
26 |
+
"""Generates the chatbot response."""
|
27 |
+
chat_formatted = chat_with_ai(message, history, system_prompt)
|
28 |
+
response, thinking_time = respond(message, chat_formatted, model, system_prompt, thinking_budget)
|
29 |
+
history.append({"user": message, "assistant": response})
|
30 |
+
return history, ""
|
31 |
+
|
32 |
+
# Define the default system prompt
|
33 |
DEFAULT_SYSTEM_PROMPT = """
|
34 |
You are a helpful assistant in normal conversation.
|
35 |
When given a problem to solve, you are an expert problem-solving assistant.
|
36 |
Your task is to provide a detailed, step-by-step solution to a given question.
|
37 |
"""
|
38 |
|
|
|
|
|
|
|
|
|
|
|
39 |
with gr.Blocks() as demo:
|
40 |
gr.Markdown("# Custom Chat Interface")
|
41 |
+
|
42 |
with gr.Row():
|
43 |
+
model = gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[0])
|
44 |
+
thinking_budget = gr.Slider(minimum=1, maximum=100, value=10, step=1, label="Thinking Budget")
|
45 |
+
|
46 |
+
system_prompt = gr.Textbox(value=DEFAULT_SYSTEM_PROMPT, lines=15, label="System Prompt")
|
47 |
+
chatbot = gr.Chatbot(label="Chat", type="messages")
|
48 |
msg = gr.Textbox(label="Type your message here...", placeholder="Enter your message...")
|
49 |
+
|
50 |
+
# Clear chat function
|
51 |
+
def clear_chat():
|
52 |
+
return [], ""
|
53 |
+
|
54 |
gr.Button("Clear Chat").click(clear_chat, inputs=None, outputs=[chatbot, msg])
|
55 |
|
56 |
+
# Generate response on message submission
|
57 |
+
msg.submit(generate, inputs=[msg, chatbot, model, system_prompt, thinking_budget], outputs=[chatbot, msg])
|
58 |
+
|
59 |
demo.launch()
|