Spaces:
Build error
Build error
ffreemt
commited on
Commit
·
93c9911
1
Parent(s):
26b527f
ncc
Browse files- README.md +1 -1
- __pycache__/examples_list.cpython-310.pyc +0 -0
- app-org.py +50 -0
- app.py +60 -10
- examples_list.py +42 -0
README.md
CHANGED
|
@@ -5,7 +5,7 @@ colorFrom: yellow
|
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 3.39.0
|
| 8 |
-
app_file: app.py
|
| 9 |
pinned: true
|
| 10 |
---
|
| 11 |
|
|
|
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 3.39.0
|
| 8 |
+
app_file: app-org.py
|
| 9 |
pinned: true
|
| 10 |
---
|
| 11 |
|
__pycache__/examples_list.cpython-310.pyc
ADDED
|
Binary file (2.92 kB). View file
|
|
|
app-org.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Try out gradio.Chatinterface.
|
| 2 |
+
|
| 3 |
+
colab gradio-chatinterface.
|
| 4 |
+
|
| 5 |
+
%%writefile reuirements.txt
|
| 6 |
+
gradio
|
| 7 |
+
transformers
|
| 8 |
+
sentencepiece
|
| 9 |
+
torch
|
| 10 |
+
|
| 11 |
+
"""
|
| 12 |
+
# pylint: disable=line-too-long, missing-module-docstring, missing-function-docstring
|
| 13 |
+
# import torch
|
| 14 |
+
import gradio as gr
|
| 15 |
+
from transformers import AutoModel, AutoTokenizer # AutoModelForCausalLM,
|
| 16 |
+
|
| 17 |
+
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
|
| 19 |
+
# tokenizer = AutoTokenizer.from_pretrained("stabilityai/StableBeluga2", use_fast=False)
|
| 20 |
+
# model = AutoModelForCausalLM.from_pretrained("stabilityai/StableBeluga2", torch_dtype=torch.float16, low_cpu_mem_usage=True, device_map="auto")
|
| 21 |
+
# system_prompt = "### System:\nYou are Stable Beluga, an AI that follows instructions extremely well. Help as much as you can. Remember, be safe, and don't do anything illegal.\n\n"
|
| 22 |
+
# pipeline = pipeline(task="text-generation", model="meta-llama/Llama-2-7b")
|
| 23 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 24 |
+
"THUDM/chatglm2-6b-int4", trust_remote_code=True
|
| 25 |
+
)
|
| 26 |
+
chat_model = AutoModel.from_pretrained(
|
| 27 |
+
"THUDM/chatglm2-6b-int4", trust_remote_code=True # 3.92G
|
| 28 |
+
).float()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def chat(message, history):
|
| 32 |
+
# prompt = f"{system_prompt}### User: {message}\n\n### Assistant:\n"
|
| 33 |
+
# inputs = tokenizer(prompt, return_tensors="pt").to(device=device)
|
| 34 |
+
# output = model.generate(**inputs, do_sample=True, top_p=0.95, top_k=0, max_new_tokens=256)
|
| 35 |
+
# return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 36 |
+
for response, _ in chat_model.stream_chat(
|
| 37 |
+
tokenizer, message, history, max_length=2048, top_p=0.7, temperature=0.95
|
| 38 |
+
):
|
| 39 |
+
yield response
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
gr.ChatInterface(
|
| 43 |
+
chat,
|
| 44 |
+
title="gradio-chatinterface-tryout",
|
| 45 |
+
# description="fooling around",
|
| 46 |
+
examples=[
|
| 47 |
+
["test me"],
|
| 48 |
+
],
|
| 49 |
+
theme=gr.themes.Glass(text_size="sm", spacing_size="sm"),
|
| 50 |
+
).queue(max_size=2).launch()
|
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
"""
|
|
|
|
| 2 |
|
| 3 |
colab gradio-chatinterface.
|
| 4 |
|
|
@@ -12,6 +13,7 @@ torch
|
|
| 12 |
# pylint: disable=line-too-long, missing-module-docstring, missing-function-docstring
|
| 13 |
# import torch
|
| 14 |
import gradio as gr
|
|
|
|
| 15 |
from transformers import AutoModel, AutoTokenizer # AutoModelForCausalLM,
|
| 16 |
|
| 17 |
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -20,15 +22,61 @@ from transformers import AutoModel, AutoTokenizer # AutoModelForCausalLM,
|
|
| 20 |
# model = AutoModelForCausalLM.from_pretrained("stabilityai/StableBeluga2", torch_dtype=torch.float16, low_cpu_mem_usage=True, device_map="auto")
|
| 21 |
# system_prompt = "### System:\nYou are Stable Beluga, an AI that follows instructions extremely well. Help as much as you can. Remember, be safe, and don't do anything illegal.\n\n"
|
| 22 |
# pipeline = pipeline(task="text-generation", model="meta-llama/Llama-2-7b")
|
|
|
|
|
|
|
| 23 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 24 |
"THUDM/chatglm2-6b-int4", trust_remote_code=True
|
| 25 |
)
|
| 26 |
chat_model = AutoModel.from_pretrained(
|
| 27 |
"THUDM/chatglm2-6b-int4", trust_remote_code=True # 3.92G
|
| 28 |
).float()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
-
def
|
| 32 |
# prompt = f"{system_prompt}### User: {message}\n\n### Assistant:\n"
|
| 33 |
# inputs = tokenizer(prompt, return_tensors="pt").to(device=device)
|
| 34 |
# output = model.generate(**inputs, do_sample=True, top_p=0.95, top_k=0, max_new_tokens=256)
|
|
@@ -36,15 +84,17 @@ def chat(message, history):
|
|
| 36 |
for response, _ in chat_model.stream_chat(
|
| 37 |
tokenizer, message, history, max_length=2048, top_p=0.7, temperature=0.95
|
| 38 |
):
|
| 39 |
-
yield response
|
| 40 |
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
gr.ChatInterface(
|
|
|
|
| 43 |
chat,
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
],
|
| 49 |
-
theme=gr.themes.Glass(text_size="sm", spacing_size="sm"),
|
| 50 |
).queue(max_size=2).launch()
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Try out gradio.Chatinterface.
|
| 3 |
|
| 4 |
colab gradio-chatinterface.
|
| 5 |
|
|
|
|
| 13 |
# pylint: disable=line-too-long, missing-module-docstring, missing-function-docstring
|
| 14 |
# import torch
|
| 15 |
import gradio as gr
|
| 16 |
+
from examples_list import examples_list
|
| 17 |
from transformers import AutoModel, AutoTokenizer # AutoModelForCausalLM,
|
| 18 |
|
| 19 |
# device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 22 |
# model = AutoModelForCausalLM.from_pretrained("stabilityai/StableBeluga2", torch_dtype=torch.float16, low_cpu_mem_usage=True, device_map="auto")
|
| 23 |
# system_prompt = "### System:\nYou are Stable Beluga, an AI that follows instructions extremely well. Help as much as you can. Remember, be safe, and don't do anything illegal.\n\n"
|
| 24 |
# pipeline = pipeline(task="text-generation", model="meta-llama/Llama-2-7b")
|
| 25 |
+
|
| 26 |
+
_ = """
|
| 27 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 28 |
"THUDM/chatglm2-6b-int4", trust_remote_code=True
|
| 29 |
)
|
| 30 |
chat_model = AutoModel.from_pretrained(
|
| 31 |
"THUDM/chatglm2-6b-int4", trust_remote_code=True # 3.92G
|
| 32 |
).float()
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
def stream_chat():
|
| 36 |
+
"""samples:
|
| 37 |
+
|
| 38 |
+
Sure [('test me', 'Sure')]
|
| 39 |
+
Sure, [('test me', 'Sure,')]
|
| 40 |
+
Sure, I [('test me', 'Sure, I')]
|
| 41 |
+
Sure, I' [('test me', "Sure, I'")]
|
| 42 |
+
Sure, I'd [('test me', "Sure, I'd")]
|
| 43 |
+
"""
|
| 44 |
+
resp = ""
|
| 45 |
+
for elm in range(10):
|
| 46 |
+
resp += str(elm)
|
| 47 |
+
from time import sleep
|
| 48 |
+
sleep(0.1)
|
| 49 |
+
yield elm
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
def chat(message="", history=[]):
|
| 53 |
+
# prompt = f"{system_prompt}### User: {message}\n\n### Assistant:\n"
|
| 54 |
+
# inputs = tokenizer(prompt, return_tensors="pt").to(device=device)
|
| 55 |
+
# output = model.generate(**inputs, do_sample=True, top_p=0.95, top_k=0, max_new_tokens=256)
|
| 56 |
+
# return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 57 |
+
_ = """
|
| 58 |
+
for response, _ in chat_model.stream_chat(
|
| 59 |
+
tokenizer, message, history, max_length=2048, top_p=0.7, temperature=0.95
|
| 60 |
+
):
|
| 61 |
+
yield response
|
| 62 |
+
"""
|
| 63 |
+
g = update_chatbot()
|
| 64 |
+
g.send(None)
|
| 65 |
+
|
| 66 |
+
for response in stream_chat():
|
| 67 |
+
# yield response
|
| 68 |
+
g.send(response)
|
| 69 |
+
|
| 70 |
+
return history
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def update_chatbot():
|
| 74 |
+
while 1:
|
| 75 |
+
message = yield
|
| 76 |
+
print(f"{message=}")
|
| 77 |
|
| 78 |
|
| 79 |
+
def chat1(message, history):
|
| 80 |
# prompt = f"{system_prompt}### User: {message}\n\n### Assistant:\n"
|
| 81 |
# inputs = tokenizer(prompt, return_tensors="pt").to(device=device)
|
| 82 |
# output = model.generate(**inputs, do_sample=True, top_p=0.95, top_k=0, max_new_tokens=256)
|
|
|
|
| 84 |
for response, _ in chat_model.stream_chat(
|
| 85 |
tokenizer, message, history, max_length=2048, top_p=0.7, temperature=0.95
|
| 86 |
):
|
| 87 |
+
yield response, _
|
| 88 |
|
| 89 |
+
with gr.Blocks(theme=gr.themes.Glass(text_size="sm", spacing_size="sm"),) as block:
|
| 90 |
+
chatbot = gr.Chatbot()
|
| 91 |
+
msg = gr.Textbox()
|
| 92 |
|
| 93 |
+
# gr.ChatInterface(
|
| 94 |
+
block(
|
| 95 |
chat,
|
| 96 |
+
[msg, chatbot],
|
| 97 |
+
[chatbot],
|
| 98 |
+
# title="gradio-chatinterface-tryout",
|
| 99 |
+
# examples=examples_list,
|
|
|
|
|
|
|
| 100 |
).queue(max_size=2).launch()
|
examples_list.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
etext = """In America, where cars are an important part of the national psyche, a decade ago people had suddenly started to drive less, which had not happened since the oil shocks of the 1970s. """
|
| 2 |
+
examples_list = [
|
| 3 |
+
["What NFL team won the Super Bowl in the year Justin Bieber was born?"],
|
| 4 |
+
[
|
| 5 |
+
"What NFL team won the Super Bowl in the year Justin Bieber was born? Think step by step."
|
| 6 |
+
],
|
| 7 |
+
["How to pick a lock? Provide detailed steps."],
|
| 8 |
+
[ "If it takes 10 hours to dry 10 clothes, assuming all the clothes are hung together at the same time for drying , then how long will it take to dry a cloth?"
|
| 9 |
+
],
|
| 10 |
+
[
|
| 11 |
+
"If it takes 10 hours to dry 10 clothes, assuming all the clothes are hung together at the same time for drying , then how long will it take to dry a cloth? Think step by step."
|
| 12 |
+
],
|
| 13 |
+
["is infinity + 1 bigger than infinity?"],
|
| 14 |
+
["Explain the plot of Cinderella in a sentence."],
|
| 15 |
+
[
|
| 16 |
+
"How long does it take to become proficient in French, and what are the best methods for retaining information?"
|
| 17 |
+
],
|
| 18 |
+
["What are some common mistakes to avoid when writing code?"],
|
| 19 |
+
["Build a prompt to generate a beautiful portrait of a horse"],
|
| 20 |
+
["Suggest four metaphors to describe the benefits of AI"],
|
| 21 |
+
["Write a pop song about leaving home for the sandy beaches."],
|
| 22 |
+
["Write a summary demonstrating my ability to tame lions"],
|
| 23 |
+
["鲁迅和周树人什么关系? 说中文。"],
|
| 24 |
+
["鲁迅和周树人什么关系?"],
|
| 25 |
+
["鲁迅和周树人什么关系? 用英文回答。"],
|
| 26 |
+
["从前有一头牛,这头牛后面有什么?"],
|
| 27 |
+
["正无穷大加一大于正无穷大吗?"],
|
| 28 |
+
["正无穷大加正无穷大大于正无穷大吗?"],
|
| 29 |
+
["-2的平方根等于什么?"],
|
| 30 |
+
["树上有5只鸟,猎人开枪打死了一只。树上还有几只鸟?"],
|
| 31 |
+
["树上有11只鸟,猎人开枪打死了一只。树上还有几只鸟?提示:需考虑鸟可能受惊吓飞走。"],
|
| 32 |
+
["以红楼梦的行文风格写一张委婉的请假条。不少于320字。"],
|
| 33 |
+
[f"Translate ths following to Chinese. List 2 variants: \n{etext}"],
|
| 34 |
+
[f"{etext} 翻成中文,列出3个版本。"],
|
| 35 |
+
[f"{etext} \n 翻成中文,保留原意,但使用文学性的语言。不要写解释。列出3个版本。"],
|
| 36 |
+
["假定 1 + 2 = 4, 试求 7 + 8。"],
|
| 37 |
+
["给出判断一个数是不是质数的 javascript 码。"],
|
| 38 |
+
["给出实现python 里 range(10)的 javascript 码。"],
|
| 39 |
+
["给出实现python 里 [*(range(10)]的 javascript 码。"],
|
| 40 |
+
["Erkläre die Handlung von Cinderella in einem Satz."],
|
| 41 |
+
["Erkläre die Handlung von Cinderella in einem Satz. Auf Deutsch."],
|
| 42 |
+
]
|