Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,49 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True)
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 8 |
|
| 9 |
def respond(message, history: list[tuple[str, str]]):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Prepare the conversation history
|
| 11 |
-
messages = []
|
|
|
|
| 12 |
for user_msg, assistant_msg in history:
|
| 13 |
if user_msg:
|
| 14 |
messages.append({"role": "user", "content": user_msg})
|
| 15 |
if assistant_msg:
|
| 16 |
messages.append({"role": "assistant", "content": assistant_msg})
|
| 17 |
-
messages.append({"role": "user", "content": message})
|
| 18 |
-
|
| 19 |
-
# Tokenize the input
|
| 20 |
-
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt")
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 25 |
-
|
| 26 |
-
return response
|
| 27 |
-
|
| 28 |
-
# Custom ChatInterface with undo and retry buttons
|
| 29 |
-
def chat_interface(message, history):
|
| 30 |
-
return respond(message, history)
|
| 31 |
|
| 32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
demo = gr.ChatInterface(
|
| 34 |
-
fn=
|
| 35 |
-
retry_btn="Retry",
|
| 36 |
-
undo_btn="Undo",
|
| 37 |
-
clear_btn="Clear",
|
| 38 |
)
|
| 39 |
|
|
|
|
| 40 |
if __name__ == "__main__":
|
| 41 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# Initialize the InferenceClient with the model hosted on Hugging Face Hub
|
| 5 |
+
client = InferenceClient("deepseek-ai/DeepSeek-R1")
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def respond(message, history: list[tuple[str, str]]):
|
| 8 |
+
# System message and generation parameters
|
| 9 |
+
system_message = "You are a friendly Chatbot."
|
| 10 |
+
max_tokens = 2048
|
| 11 |
+
temperature = 0.7
|
| 12 |
+
top_p = 0.95
|
| 13 |
+
|
| 14 |
# Prepare the conversation history
|
| 15 |
+
messages = [{"role": "system", "content": system_message}]
|
| 16 |
+
|
| 17 |
for user_msg, assistant_msg in history:
|
| 18 |
if user_msg:
|
| 19 |
messages.append({"role": "user", "content": user_msg})
|
| 20 |
if assistant_msg:
|
| 21 |
messages.append({"role": "assistant", "content": assistant_msg})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Add the current user message
|
| 24 |
+
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Stream the response from the model
|
| 27 |
+
response = ""
|
| 28 |
+
for message in client.chat_completion(
|
| 29 |
+
messages,
|
| 30 |
+
max_tokens=max_tokens,
|
| 31 |
+
stream=True,
|
| 32 |
+
temperature=temperature,
|
| 33 |
+
top_p=top_p,
|
| 34 |
+
):
|
| 35 |
+
token = message.choices[0].delta.content
|
| 36 |
+
response += token
|
| 37 |
+
yield response
|
| 38 |
+
|
| 39 |
+
# Create the Gradio ChatInterface with Retry, Undo, and Clear buttons
|
| 40 |
demo = gr.ChatInterface(
|
| 41 |
+
fn=respond, # Function to handle chat responses
|
| 42 |
+
retry_btn="Retry", # Add a Retry button
|
| 43 |
+
undo_btn="Undo", # Add an Undo button
|
| 44 |
+
clear_btn="Clear", # Add a Clear button
|
| 45 |
)
|
| 46 |
|
| 47 |
+
# Launch the Gradio app
|
| 48 |
if __name__ == "__main__":
|
| 49 |
demo.launch()
|