Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,156 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
message,
|
| 12 |
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
|
|
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import uuid
|
| 3 |
+
import json
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
|
| 6 |
+
# Load the DeepSeek-R1 model
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
"deepseek-ai/DeepSeek-R1",
|
| 9 |
+
trust_remote_code=True,
|
| 10 |
+
)
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-R1")
|
| 12 |
+
|
| 13 |
+
def format_messages_for_model(messages):
|
| 14 |
+
"""
|
| 15 |
+
Format the conversation history into a single string compatible with the DeepSeek-R1 model.
|
| 16 |
+
"""
|
| 17 |
+
formatted_messages = ""
|
| 18 |
+
for msg in messages:
|
| 19 |
+
if msg["role"] == "user":
|
| 20 |
+
formatted_messages += f"User: {msg['content']}\n"
|
| 21 |
+
elif msg["role"] == "assistant":
|
| 22 |
+
formatted_messages += f"DeepSeek-R1: {msg['content']}\n"
|
| 23 |
+
return formatted_messages
|
| 24 |
|
| 25 |
+
def generate_response(user_input, history):
|
| 26 |
+
"""
|
| 27 |
+
Generate a response using DeepSeek-R1 and return it as a string.
|
| 28 |
+
"""
|
| 29 |
+
formatted_history = format_messages_for_model(history)
|
| 30 |
+
full_input = f"{formatted_history}User: {user_input}\nDeepSeek-R1: "
|
| 31 |
|
| 32 |
+
inputs = tokenizer(full_input, return_tensors="np")
|
| 33 |
+
outputs = model.generate(
|
| 34 |
+
**inputs,
|
| 35 |
+
max_length=2048,
|
| 36 |
+
temperature=0.7,
|
| 37 |
+
top_p=0.9,
|
| 38 |
+
do_sample=True,
|
| 39 |
+
return_dict_in_generate=True,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
response = tokenizer.decode(outputs.sequences[0], skip_special_tokens=True)
|
| 43 |
+
return response.split("DeepSeek-R1: ")[-1].strip()
|
| 44 |
+
|
| 45 |
+
def chatbot(
|
| 46 |
message,
|
| 47 |
history: list[tuple[str, str]],
|
| 48 |
+
system_message: str,
|
| 49 |
+
max_tokens: int,
|
| 50 |
+
temperature: float,
|
| 51 |
+
top_p: float,
|
| 52 |
):
|
| 53 |
+
"""
|
| 54 |
+
Main function for generating responses and saving chat history.
|
| 55 |
+
"""
|
| 56 |
+
# Generate a unique session ID for this chat session
|
| 57 |
+
session_id = str(uuid.uuid4())
|
| 58 |
+
|
| 59 |
+
# Format messages for the model
|
| 60 |
+
messages = []
|
| 61 |
+
if system_message:
|
| 62 |
+
messages.append({"role": "system", "content": system_message})
|
| 63 |
+
for user_msg, bot_msg in history:
|
| 64 |
+
if user_msg:
|
| 65 |
+
messages.append({"role": "user", "content": user_msg})
|
| 66 |
+
if bot_msg:
|
| 67 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 68 |
+
|
| 69 |
+
# Generate response
|
| 70 |
+
response = generate_response(message, messages)
|
| 71 |
+
|
| 72 |
+
# Add to history
|
| 73 |
+
history.append((message, response))
|
| 74 |
+
|
| 75 |
+
# Save chat history to Hugging Face storage
|
| 76 |
+
storage = gr.HuggingFace()
|
| 77 |
+
storage.set(key=session_id, value={"messages": messages + [{"role": "assistant", "content": response}]})
|
| 78 |
+
|
| 79 |
+
return response
|
| 80 |
+
|
| 81 |
+
def create_chat_interface():
|
| 82 |
+
"""
|
| 83 |
+
Create the Gradio chat interface.
|
| 84 |
+
"""
|
| 85 |
+
with gr.Blocks() as app:
|
| 86 |
+
session_id = str(uuid.uuid4()) # Generate session ID at startup
|
| 87 |
+
|
| 88 |
+
with gr.Row():
|
| 89 |
+
chatbot_interface = gr.Chatbot(label="DeepSeek-R1 Chatbot")
|
| 90 |
+
input_box = gr.Textbox(
|
| 91 |
+
label="Type your message here",
|
| 92 |
+
placeholder="Type your message here...",
|
| 93 |
+
show_label=True,
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
# Button to submit the message
|
| 97 |
+
submit_button = gr.Button("Send")
|
| 98 |
+
|
| 99 |
+
# Store chat history in the interface
|
| 100 |
+
history = gr.State([])
|
| 101 |
+
|
| 102 |
+
# Define the system message
|
| 103 |
+
system_message = gr.Textbox(
|
| 104 |
+
value="You are DeepSeek-R1, an AI assistant.",
|
| 105 |
+
label="System Message",
|
| 106 |
+
show_label=True,
|
| 107 |
+
visible=True,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
# Define inference parameters
|
| 111 |
+
with gr.Accordion("Parameters", open=True):
|
| 112 |
+
max_tokens = gr.Slider(
|
| 113 |
+
minimum=1,
|
| 114 |
+
maximum=2048,
|
| 115 |
+
value=512,
|
| 116 |
+
step=1,
|
| 117 |
+
label="Max Tokens",
|
| 118 |
+
info="Maximum length of the generated response.",
|
| 119 |
+
)
|
| 120 |
+
temperature = gr.Slider(
|
| 121 |
+
minimum=0.0,
|
| 122 |
+
maximum=2.0,
|
| 123 |
+
value=1.0,
|
| 124 |
+
step=0.1,
|
| 125 |
+
label="Temperature",
|
| 126 |
+
info="Higher values produce more diverse outputs.",
|
| 127 |
+
)
|
| 128 |
+
top_p = gr.Slider(
|
| 129 |
+
minimum=0.1,
|
| 130 |
+
maximum=1.0,
|
| 131 |
+
value=0.9,
|
| 132 |
+
step=0.05,
|
| 133 |
+
label="Top-p",
|
| 134 |
+
info="Controls diversity through nucleus sampling.",
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
# Define the submit function
|
| 138 |
+
submit_button.click(
|
| 139 |
+
fn=chatbot,
|
| 140 |
+
inputs=[
|
| 141 |
+
input_box,
|
| 142 |
+
history,
|
| 143 |
+
system_message,
|
| 144 |
+
max_tokens,
|
| 145 |
+
temperature,
|
| 146 |
+
top_p,
|
| 147 |
+
],
|
| 148 |
+
outputs=[gr.Textbox(), chatbot_interface],
|
| 149 |
+
api_name="chatbot",
|
| 150 |
+
)
|
| 151 |
|
| 152 |
+
return app
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
+
app = create_chat_interface()
|
| 156 |
+
app.launch()
|