Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,70 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
-
|
| 20 |
-
for val in history:
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
-
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
|
| 4 |
+
# Load model and tokenizer
|
| 5 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-small")
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-small")
|
|
|
|
| 7 |
|
| 8 |
+
def chat_response(message, history):
|
| 9 |
+
try:
|
| 10 |
+
# Build conversation history string
|
| 11 |
+
chat_history_ids = None
|
| 12 |
+
for human_msg, bot_msg in history:
|
| 13 |
+
# Encode user message
|
| 14 |
+
user_input_ids = tokenizer.encode(
|
| 15 |
+
human_msg + tokenizer.eos_token,
|
| 16 |
+
return_tensors='pt'
|
| 17 |
+
)
|
| 18 |
+
# Encode bot response
|
| 19 |
+
bot_output_ids = tokenizer.encode(
|
| 20 |
+
bot_msg + tokenizer.eos_token,
|
| 21 |
+
return_tensors='pt'
|
| 22 |
+
)
|
| 23 |
+
# Build full conversation
|
| 24 |
+
if chat_history_ids is None:
|
| 25 |
+
chat_history_ids = torch.cat([user_input_ids, bot_output_ids], dim=-1)
|
| 26 |
+
else:
|
| 27 |
+
chat_history_ids = torch.cat([chat_history_ids, user_input_ids, bot_output_ids], dim=-1)
|
| 28 |
+
|
| 29 |
+
# Add new user message
|
| 30 |
+
new_user_input_ids = tokenizer.encode(
|
| 31 |
+
message + tokenizer.eos_token,
|
| 32 |
+
return_tensors='pt'
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Generate response
|
| 36 |
+
chat_history_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if chat_history_ids is not None else new_user_input_ids
|
| 37 |
+
|
| 38 |
+
# Generate bot response
|
| 39 |
+
bot_output_ids = model.generate(
|
| 40 |
+
chat_history_ids,
|
| 41 |
+
max_length=1000,
|
| 42 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 43 |
+
no_repeat_ngram_size=3,
|
| 44 |
+
do_sample=True,
|
| 45 |
+
top_k=100,
|
| 46 |
+
top_p=0.7,
|
| 47 |
+
temperature=0.8
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Extract only the bot's response (remove history)
|
| 51 |
+
response = tokenizer.decode(
|
| 52 |
+
bot_output_ids[:, chat_history_ids.shape[-1]:][0],
|
| 53 |
+
skip_special_tokens=True
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
return response
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Error: {str(e)}"
|
| 60 |
|
| 61 |
+
# Create chat interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
demo = gr.ChatInterface(
|
| 63 |
+
chat_response,
|
| 64 |
+
title="DialoGPT Chatbot",
|
| 65 |
+
examples=["Hello!", "What's AI?", "Tell me a joke"],
|
| 66 |
+
type="messages"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
)
|
| 68 |
|
|
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
+
demo.launch()
|