Spaces:
Sleeping
Sleeping
Changed Model
Browse files
app.py
CHANGED
@@ -1,12 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
|
4 |
-
"""
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
9 |
|
|
|
10 |
def respond(
|
11 |
message,
|
12 |
history: list[tuple[str, str]],
|
@@ -15,34 +16,28 @@ def respond(
|
|
15 |
temperature,
|
16 |
top_p,
|
17 |
):
|
18 |
-
|
19 |
-
|
20 |
-
for
|
21 |
-
if
|
22 |
-
|
23 |
-
if
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
temperature=temperature,
|
35 |
top_p=top_p,
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
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 |
respond,
|
48 |
additional_inputs=[
|
@@ -59,6 +54,7 @@ demo = gr.ChatInterface(
|
|
59 |
],
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# Load DialoGPT model and tokenizer
|
6 |
+
model_name = "microsoft/DialoGPT-large"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
|
10 |
+
# Respond function for Gradio interface
|
11 |
def respond(
|
12 |
message,
|
13 |
history: list[tuple[str, str]],
|
|
|
16 |
temperature,
|
17 |
top_p,
|
18 |
):
|
19 |
+
# Format the chat history for the DialoGPT model
|
20 |
+
full_conversation = ""
|
21 |
+
for user_msg, bot_msg in history:
|
22 |
+
if user_msg:
|
23 |
+
full_conversation += f"User: {user_msg}\n"
|
24 |
+
if bot_msg:
|
25 |
+
full_conversation += f"DialoGPT: {bot_msg}\n"
|
26 |
+
full_conversation += f"User: {message}\nDialoGPT:"
|
27 |
+
|
28 |
+
# Tokenize input and generate response
|
29 |
+
inputs = tokenizer.encode(full_conversation, return_tensors="pt")
|
30 |
+
outputs = model.generate(
|
31 |
+
inputs,
|
32 |
+
max_length=max_tokens,
|
|
|
|
|
33 |
temperature=temperature,
|
34 |
top_p=top_p,
|
35 |
+
pad_token_id=tokenizer.eos_token_id,
|
36 |
+
)
|
37 |
+
response = tokenizer.decode(outputs[:, inputs.shape[-1] :][0], skip_special_tokens=True)
|
38 |
+
return response
|
|
|
39 |
|
40 |
+
# Gradio Chat Interface
|
|
|
|
|
|
|
41 |
demo = gr.ChatInterface(
|
42 |
respond,
|
43 |
additional_inputs=[
|
|
|
54 |
],
|
55 |
)
|
56 |
|
|
|
57 |
if __name__ == "__main__":
|
58 |
+
# Launch the Gradio app with API enabled
|
59 |
+
demo.launch(enable_api=True)
|
60 |
+
|