Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,72 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
response =
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 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 |
-
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load a lightweight, conversational model
|
| 6 |
+
model_name = "microsoft/DialoGPT-small"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
# Store chat history
|
| 11 |
+
chat_history_ids = None
|
| 12 |
|
| 13 |
+
# Inference function
|
| 14 |
+
def chat_with_bot(user_input, history=[]):
|
| 15 |
+
global chat_history_ids
|
| 16 |
+
new_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
if chat_history_ids is not None:
|
| 19 |
+
bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1)
|
| 20 |
+
else:
|
| 21 |
+
bot_input_ids = new_input_ids
|
|
|
|
| 22 |
|
| 23 |
+
chat_history_ids = model.generate(
|
| 24 |
+
bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id,
|
| 25 |
+
no_repeat_ngram_size=3, do_sample=True, top_k=100, top_p=0.7, temperature=0.8
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 29 |
+
history.append((user_input, response))
|
| 30 |
+
return history, history
|
| 31 |
|
| 32 |
+
# Gradio Interface
|
| 33 |
+
def reset_history():
|
| 34 |
+
global chat_history_ids
|
| 35 |
+
chat_history_ids = None
|
| 36 |
+
return []
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
theme_css = """
|
| 39 |
+
body {
|
| 40 |
+
background-color: #FFF3E0;
|
| 41 |
+
font-family: Arial, sans-serif;
|
| 42 |
+
}
|
| 43 |
|
| 44 |
+
h1 {
|
| 45 |
+
color: darkorange;
|
| 46 |
+
text-align: center;
|
| 47 |
+
font-weight: bold;
|
| 48 |
+
}
|
| 49 |
|
| 50 |
+
.gradio-container {
|
| 51 |
+
max-width: 900px;
|
| 52 |
+
margin: auto;
|
| 53 |
+
padding: 20px;
|
| 54 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
|
| 55 |
+
border-radius: 12px;
|
| 56 |
+
background-color: white;
|
| 57 |
+
}
|
| 58 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
with gr.Blocks(css=theme_css) as demo:
|
| 61 |
+
gr.Markdown("""<h1>🤖 AI Chatbot Assistant</h1>""")
|
| 62 |
+
|
| 63 |
+
chatbot = gr.Chatbot()
|
| 64 |
+
msg = gr.Textbox(placeholder="Type your message here...", label="Your Message")
|
| 65 |
+
clear = gr.Button("🧹 Clear Chat")
|
| 66 |
+
|
| 67 |
+
state = gr.State([])
|
| 68 |
+
|
| 69 |
+
msg.submit(chat_with_bot, [msg, state], [chatbot, state])
|
| 70 |
+
clear.click(fn=reset_history, outputs=[chatbot, state])
|
| 71 |
|
| 72 |
+
demo.launch()
|
|
|