cheesexuebao commited on
Commit
a20001f
1 Parent(s): 9cbb816

test upload and download file

Browse files
Files changed (1) hide show
  1. app.py +51 -64
app.py CHANGED
@@ -1,67 +1,54 @@
1
  import gradio as gr
2
- import torch
3
- from transformers import BertModel
4
- from transformers import BertTokenizer
5
- import torch.nn as nn
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- class BertSST2Model(nn.Module):
8
-
9
- # 初始化类
10
- def __init__(self, class_size, pretrained_name='bert-base-chinese'):
11
- """
12
- Args:
13
- class_size :指定分类模型的最终类别数目,以确定线性分类器的映射维度
14
- pretrained_name :用以指定bert的预训练模型
15
- """
16
- # 类继承的初始化,固定写法
17
- super(BertSST2Model, self).__init__()
18
- # 加载HuggingFace的BertModel
19
- # BertModel的最终输出维度默认为768
20
- # return_dict=True 可以使BertModel的输出具有dict属性,即以 bert_output['last_hidden_state'] 方式调用
21
- self.bert = BertModel.from_pretrained(pretrained_name,
22
- return_dict=True)
23
- # 通过一个线性层将[CLS]标签对应的维度:768->class_size
24
- # class_size 在SST-2情感分类任务中设置为:2
25
- self.classifier = nn.Linear(768, class_size)
26
-
27
- def forward(self, inputs):
28
- # 获取DataLoader中已经处理好的输入数据:
29
- # input_ids :tensor类型,shape=batch_size*max_len max_len为当前batch中的最大句长
30
- # input_tyi :tensor类型,
31
- # input_attn_mask :tensor类型,因为input_ids中存在大量[Pad]填充,attention mask将pad部分值置为0,让模型只关注非pad部分
32
- input_ids, input_tyi, input_attn_mask = inputs['input_ids'], inputs[
33
- 'token_type_ids'], inputs['attention_mask']
34
- # 将三者输入进模型,如果想知道模型内部如何运作,前面的蛆以后再来探索吧~
35
- output = self.bert(input_ids, input_tyi, input_attn_mask)
36
- # bert_output 分为两个部分:
37
- # last_hidden_state:最后一个隐层的值
38
- # pooler output:对应的是[CLS]的输出,用于分类任务
39
- # 通过线性层将维度:768->2
40
- # categories_numberic:tensor类型,shape=batch_size*class_size,用于后续的CrossEntropy计算
41
- categories_numberic = self.classifier(output.pooler_output)
42
- return categories_numberic
43
-
44
-
45
- device = torch.device("cpu")
46
- pretrained_model_name = './bert-base-uncased'
47
- # 创建模型 BertSST2Model
48
- model = BertSST2Model(2, pretrained_model_name)
49
- # 固定写法,将模型加载到device上,
50
- # 如果是GPU上运行,此时可以观察到GPU的显存增加
51
- model.to(device)
52
- # 加载预训练模型对应的tokenizer
53
- tokenizer = BertTokenizer.from_pretrained(pretrained_model_name)
54
-
55
-
56
- def modelscope_quickstart(sentence):
57
- inputs = tokenizer(sentence,
58
- padding=True,
59
- truncation=True,
60
- return_tensors="pt",
61
- max_length=512)
62
- output = model(inputs)
63
- cate = output.argmax(dim=1)
64
- return "分类结果为:" + str(0 if cate else 1)
65
-
66
- demo = gr.Interface(fn=modelscope_quickstart, inputs="text", outputs="text")
67
  demo.launch()
 
1
  import gradio as gr
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ # from Prediction import *
5
+
6
+ # 定义处理函数
7
+ def process_data(csv_file, num, model_name):
8
+ # 读取CSV文件
9
+ df = pd.read_csv(csv_file.name)
10
+
11
+ # 处理数据
12
+ processed_data = df * num
13
+
14
+ # 生成图片
15
+ plt.plot(processed_data)
16
+ plt.xlabel('X Label')
17
+ plt.ylabel('Y Label')
18
+ plt.title('Processed Data')
19
+ plt.grid(True)
20
+ plt.savefig('output.png')
21
+ plt.close()
22
+
23
+ # 生成字符串结果
24
+ result = pd.DataFrame({'result':[1,2,3]})
25
+ result.to_csv('output.csv')
26
+ print(model_name)
27
+ return 'output.csv', ['output.png','output.png']
28
+
29
+ my_theme = gr.Theme.from_hub("gstaff/sketch")
30
+ with gr.Blocks(theme=my_theme, title='Test') as demo:
31
+ gr.Markdown("""# Test
32
+ xxxx
33
+ """)
34
+
35
+ with gr.Tab("Single Sentence"):
36
+ with gr.Column():
37
+ csv_input = gr.File(label="CSV文件")
38
+ text_output = gr.File(label="结果")
39
+ image_output = gr.Gallery(label="图像")
40
+ with gr.Row():
41
+ seed_input = gr.Slider(minimum=0, maximum=100, step=1, label="seed",info="Different seeds may generate different results")
42
+ model_input = gr.CheckboxGroup(["ALL_Data", "Facebook", "Kickstarter", "Twitter"], label="Countries", info="Where are they from?")
43
+
44
+ with gr.Row():
45
+ button = gr.Button("Submit", variant="primary")
46
+ button.click(fn=process_data, inputs=[csv_input, seed_input, model_input], outputs=[text_output, image_output])
47
+ clear = gr.ClearButton([csv_input, text_output, image_output])
48
+
49
+ with gr.Tab("Csv File"):
50
+ ...
51
+ with gr.Tab("README"):
52
+ seed_input = gr.Textbox(label="seed")
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  demo.launch()