cofeg commited on
Commit
0f90cb6
·
verified ·
1 Parent(s): 09549fa

feat: deploy app for ancient Chinese text generation

Browse files
Files changed (1) hide show
  1. app.py +24 -0
app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ responses = ""
14
+ for sentence in sentences:
15
+ input = "现代文:" + sentence + " 古文:"
16
+ for token in client.text_generation(input, max_new_tokens=128, stream=True):
17
+ if token != "<|endoftext|>":
18
+ responses += token
19
+ yield responses
20
+
21
+ demo = gr.Interface(fn=split_and_generate,
22
+ inputs=[gr.Textbox(label="现代文", lines=10)],
23
+ outputs=[gr.Textbox(label="古文", lines=10)])
24
+ demo.launch()