Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,37 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
3 |
-
import torch
|
4 |
|
5 |
-
# Load
|
6 |
-
tokenizer = AutoTokenizer.from_pretrained("microsoft/
|
7 |
-
model =
|
8 |
|
9 |
-
# Define chatbot function
|
10 |
-
def
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
conversation += turn["content"] + tokenizer.eos_token
|
16 |
-
elif turn["role"] == "assistant":
|
17 |
-
conversation += turn["content"] + tokenizer.eos_token
|
18 |
|
19 |
-
#
|
20 |
-
|
|
|
21 |
|
22 |
# Tokenize and generate
|
23 |
-
input_ids = tokenizer
|
24 |
output_ids = model.generate(
|
25 |
input_ids,
|
26 |
-
max_length=
|
27 |
-
pad_token_id=tokenizer.eos_token_id,
|
28 |
do_sample=True,
|
29 |
-
|
30 |
-
|
31 |
)
|
32 |
-
|
33 |
-
# Decode only the new response
|
34 |
-
response = tokenizer.decode(output_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
35 |
-
|
36 |
return {"role": "assistant", "content": response}
|
37 |
|
38 |
# Launch the chatbot
|
39 |
gr.ChatInterface(
|
40 |
-
fn=
|
41 |
-
title="Muhammad’s
|
42 |
-
description="A
|
43 |
-
type="messages"
|
44 |
).launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
|
|
3 |
|
4 |
+
# Load GODEL model and tokenizer
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/GODEL-v1_1-base-seq2seq")
|
6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("microsoft/GODEL-v1_1-base-seq2seq")
|
7 |
|
8 |
+
# Define the chatbot function
|
9 |
+
def generate_response(message, history):
|
10 |
+
# Format dialog history
|
11 |
+
dialog = [turn["content"] for turn in history if turn["role"] == "user"]
|
12 |
+
dialog.append(message)
|
13 |
+
dialog_text = " EOS ".join(dialog)
|
|
|
|
|
|
|
14 |
|
15 |
+
# GODEL expects an instruction and context
|
16 |
+
instruction = "Instruction: given a dialog context, respond appropriately."
|
17 |
+
query = f"{instruction} [CONTEXT] {dialog_text}"
|
18 |
|
19 |
# Tokenize and generate
|
20 |
+
input_ids = tokenizer(query, return_tensors="pt").input_ids
|
21 |
output_ids = model.generate(
|
22 |
input_ids,
|
23 |
+
max_length=128,
|
|
|
24 |
do_sample=True,
|
25 |
+
top_p=0.9,
|
26 |
+
temperature=0.7
|
27 |
)
|
28 |
+
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
|
|
|
|
|
|
29 |
return {"role": "assistant", "content": response}
|
30 |
|
31 |
# Launch the chatbot
|
32 |
gr.ChatInterface(
|
33 |
+
fn=generate_response,
|
34 |
+
title="Muhammad’s GODEL Chatbot",
|
35 |
+
description="A grounded chatbot powered by Microsoft's GODEL model.",
|
36 |
+
type="messages"
|
37 |
).launch()
|