Spaces:
Runtime error
Runtime error
Revert "use zero-gpu"
Browse filesThis reverts commit 5a726b802e77cb0e3a75556b7bacf25e6b4d2f3f.
- app.py +13 -35
- requirements.txt +0 -5
app.py
CHANGED
@@ -1,49 +1,27 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import re
|
4 |
-
import spaces
|
5 |
-
import torch
|
6 |
|
7 |
-
|
8 |
-
# device = "auto"
|
9 |
|
10 |
-
|
11 |
-
model = AutoModelForCausalLM.from_pretrained(
|
12 |
-
model_path,
|
13 |
-
torch_dtype="auto",
|
14 |
-
device_map=device
|
15 |
-
)
|
16 |
-
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
17 |
-
|
18 |
-
@spaces.GPU
|
19 |
-
def generate_answer(prompt, tokenizer, model):
|
20 |
-
model_inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
|
21 |
-
generated_ids = model.generate(
|
22 |
-
model_inputs.input_ids,
|
23 |
-
max_new_tokens=128
|
24 |
-
)
|
25 |
-
generated_ids = [
|
26 |
-
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
27 |
-
]
|
28 |
-
|
29 |
-
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
30 |
-
return response
|
31 |
|
32 |
-
def split_and_generate(modern_text
|
33 |
-
progress(0, desc="开始处理")
|
34 |
# Split the input text into sentences for the model is trained on sentence pairs
|
35 |
sentences = re.findall(r'[^。!?]*[。!?]', modern_text)
|
36 |
-
|
37 |
# If no sentences are found, treat the entire input as one sentence
|
38 |
if not sentences:
|
39 |
sentences = [modern_text]
|
40 |
-
|
41 |
responses = ""
|
42 |
-
for sentence in
|
43 |
input = "现代文:" + sentence + " 古文:"
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
47 |
|
48 |
demo = gr.Interface(fn=split_and_generate,
|
49 |
inputs=[gr.Textbox(label="现代文", lines=10)],
|
@@ -51,4 +29,4 @@ demo = gr.Interface(fn=split_and_generate,
|
|
51 |
title="现代文转古文大模型",
|
52 |
description="请在左边对话框输入你要转换的现代文并点击“Submit”按钮,耐心等待十几秒钟,右边的对话框将显示转换后的古文。<br>一个句子不要太长,如果文本很长,可多分几个句子,模型会逐句转化。<br>详情请访问本项目[GitHub主页](https://github.com/JianXiao2021/ancient_text_generation_LLM)。"
|
53 |
)
|
54 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
import gradio as gr
|
|
|
4 |
import re
|
|
|
|
|
5 |
|
6 |
+
hugging_face_model_path = "cofeg/Finetuned-Xunzi-Qwen2-1.5B-for-ancient-text-generation"
|
|
|
7 |
|
8 |
+
client = InferenceClient(model=hugging_face_model_path, token=os.getenv('HUGGING_FACE_TOKEN'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
def split_and_generate(modern_text):
|
|
|
11 |
# Split the input text into sentences for the model is trained on sentence pairs
|
12 |
sentences = re.findall(r'[^。!?]*[。!?]', modern_text)
|
13 |
+
|
14 |
# If no sentences are found, treat the entire input as one sentence
|
15 |
if not sentences:
|
16 |
sentences = [modern_text]
|
17 |
+
|
18 |
responses = ""
|
19 |
+
for sentence in sentences:
|
20 |
input = "现代文:" + sentence + " 古文:"
|
21 |
+
for token in client.text_generation(input, max_new_tokens=128, stream=True):
|
22 |
+
if token != "<|endoftext|>":
|
23 |
+
responses += token
|
24 |
+
yield responses
|
25 |
|
26 |
demo = gr.Interface(fn=split_and_generate,
|
27 |
inputs=[gr.Textbox(label="现代文", lines=10)],
|
|
|
29 |
title="现代文转古文大模型",
|
30 |
description="请在左边对话框输入你要转换的现代文并点击“Submit”按钮,耐心等待十几秒钟,右边的对话框将显示转换后的古文。<br>一个句子不要太长,如果文本很长,可多分几个句子,模型会逐句转化。<br>详情请访问本项目[GitHub主页](https://github.com/JianXiao2021/ancient_text_generation_LLM)。"
|
31 |
)
|
32 |
+
demo.launch()
|
requirements.txt
DELETED
@@ -1,5 +0,0 @@
|
|
1 |
-
torch
|
2 |
-
transformers
|
3 |
-
spaces
|
4 |
-
gradio
|
5 |
-
accelerate
|
|
|
|
|
|
|
|
|
|
|
|