Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
+
|
5 |
+
# 加载模型和分词器
|
6 |
+
model_name = "RWKV/rwkv-raven-1b5"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# 定义模型推断函数
|
11 |
+
def generate_text(input_text):
|
12 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
13 |
+
output = model.generate(input_ids, max_length=100, num_return_sequences=1)
|
14 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
15 |
+
return generated_text
|
16 |
+
|
17 |
+
# 创建Gradio界面
|
18 |
+
def gradio_interface():
|
19 |
+
text_input = gr.inputs.Textbox(label="输入文本")
|
20 |
+
text_output = gr.outputs.Textbox(label="生成文本")
|
21 |
+
|
22 |
+
interface = gr.Interface(
|
23 |
+
fn=generate_text,
|
24 |
+
inputs=text_input,
|
25 |
+
outputs=text_output,
|
26 |
+
title="文本生成器",
|
27 |
+
description="输入一段文本,生成相应的文本。",
|
28 |
+
theme="default"
|
29 |
+
)
|
30 |
+
return interface
|
31 |
+
|
32 |
+
|
33 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
34 |
+
iface.launch()
|
35 |
+
|
36 |
+
|