Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
|
4 |
+
# 加载模型和分词器
|
5 |
+
model_name = "CohereForAI/c4ai-command-r-plus-08-2024"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# 定义生成文本的函数
|
10 |
+
def generate_text(system_prompt, user_input):
|
11 |
+
# 将系统提示词和用户输入结合
|
12 |
+
full_prompt = f"{system_prompt}\n\n{user_input}"
|
13 |
+
|
14 |
+
# 使用模型生成文本
|
15 |
+
inputs = tokenizer(full_prompt, return_tensors="pt")
|
16 |
+
outputs = model.generate(**inputs, max_length=200)
|
17 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
18 |
+
|
19 |
+
# 创建 Gradio 界面
|
20 |
+
iface = gr.Interface(
|
21 |
+
fn=generate_text,
|
22 |
+
inputs=[
|
23 |
+
gr.Textbox(label="System Prompt", placeholder="Enter the system prompt here..."), # 系统提示词输入框
|
24 |
+
gr.Textbox(label="User Input", placeholder="Enter your input here...") # 用户输入框
|
25 |
+
],
|
26 |
+
outputs="text",
|
27 |
+
title="c4ai-command-r-plus-08-2024",
|
28 |
+
description="使用 CohereForAI/c4ai-command-r-plus-08-2024 模型生成文本。输入系统提示词和用户输入,查看模型生成的输出。"
|
29 |
+
)
|
30 |
+
|
31 |
+
# 启动应用
|
32 |
+
iface.launch()
|