Spaces:
Sleeping
Sleeping
research14
commited on
Commit
•
8c245db
1
Parent(s):
30c7cb4
test
Browse files
app.py
CHANGED
@@ -1,30 +1,28 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
|
4 |
-
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
16 |
|
17 |
-
|
18 |
-
def chatbot_interface(prompt):
|
19 |
-
vicuna_response = generate_response(vicuna_model, vicuna_tokenizer, prompt)
|
20 |
-
# llama_response = generate_response(llama_model, llama_tokenizer, prompt)
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
iface = gr.Interface(fn=chatbot_interface,
|
25 |
-
inputs="text",
|
26 |
-
outputs="text",
|
27 |
-
interpretation="default",
|
28 |
-
title="Chatbot with Three Models")
|
29 |
-
|
30 |
-
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import random
|
3 |
+
import time
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
|
6 |
+
# Load models and tokenizers
|
7 |
+
model_names = ["lmsys/vicuna-7b", "gpt2"]
|
8 |
+
models = [AutoModelForCausalLM.from_pretrained(name) for name in model_names]
|
9 |
+
tokenizers = [AutoTokenizer.from_pretrained(name) for name in model_names]
|
10 |
|
11 |
+
with gr.Blocks() as demo:
|
12 |
+
with gr.Row():
|
13 |
+
vicuna_chatbot = gr.Chatbot(label="Vicuna", live=True)
|
14 |
+
gpt2_chatbot = gr.Chatbot(label="GPT-2", live=True)
|
15 |
+
msg = gr.Textbox()
|
16 |
+
clear = gr.ClearButton([msg, vicuna_chatbot, gpt2_chatbot])
|
17 |
|
18 |
+
def respond(message, chat_history, chatbot_idx):
|
19 |
+
input_ids = tokenizers[chatbot_idx].encode(message, return_tensors="pt")
|
20 |
+
output = models[chatbot_idx].generate(input_ids, max_length=50, num_beams=5, no_repeat_ngram_size=2)
|
21 |
+
bot_message = tokenizers[chatbot_idx].decode(output[0], skip_special_tokens=True)
|
22 |
+
chat_history.append((message, bot_message))
|
23 |
+
time.sleep(2)
|
24 |
+
return "", chat_history
|
25 |
|
26 |
+
msg.submit(respond, [msg, vicuna_chatbot, 0], [msg, gpt2_chatbot, 1])
|
|
|
|
|
|
|
27 |
|
28 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test3.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
|
4 |
+
vicuna_model = AutoModelForCausalLM.from_pretrained("lmsys/vicuna-7b-v1.3")
|
5 |
+
vicuna_tokenizer = AutoTokenizer.from_pretrained("lmsys/vicuna-7b-v1.3")
|
6 |
+
|
7 |
+
# llama_model = AutoModelForCausalLM.from_pretrained("luodian/llama-7b-hf")
|
8 |
+
# llama_tokenizer = AutoTokenizer.from_pretrained("luodian/llama-7b-hf")
|
9 |
+
|
10 |
+
# Define the function for generating responses
|
11 |
+
def generate_response(model, tokenizer, prompt):
|
12 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
13 |
+
outputs = model.generate(**inputs, max_length=500, pad_token_id=tokenizer.eos_token_id)
|
14 |
+
response = tokenizer.decode(outputs[0])
|
15 |
+
return response
|
16 |
+
|
17 |
+
# Define the Gradio interface
|
18 |
+
def chatbot_interface(prompt):
|
19 |
+
vicuna_response = generate_response(vicuna_model, vicuna_tokenizer, prompt)
|
20 |
+
# llama_response = generate_response(llama_model, llama_tokenizer, prompt)
|
21 |
+
|
22 |
+
return {"Vicuna-7B": vicuna_response}
|
23 |
+
|
24 |
+
iface = gr.Interface(fn=chatbot_interface,
|
25 |
+
inputs="text",
|
26 |
+
outputs="text",
|
27 |
+
interpretation="default",
|
28 |
+
title="Chatbot with Three Models")
|
29 |
+
|
30 |
+
iface.launch()
|