kkChimmy commited on
Commit
7171165
·
verified ·
1 Parent(s): 7b38dd4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -18
app.py CHANGED
@@ -1,33 +1,25 @@
1
  import torch
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
- import gradio as gr
4
- import spaces
5
-
6
- @spaces.GPU
7
- def dummy(): # just a dummy
8
- pass
9
-
10
- # 模型和 tokenizer 的名称
11
- model_name = "./" # 假设你有权限访问Llama 3.1 8B模型
12
 
13
  # 加载模型和tokenizer
 
14
  tokenizer = AutoTokenizer.from_pretrained(model_name)
15
  model = AutoModelForCausalLM.from_pretrained(model_name)
16
 
17
  tokenizer.add_special_tokens({'pad_token': '[PAD]'})
18
- # 将模型移动到GPU(如果可用)
19
  device = "cuda" if torch.cuda.is_available() else "cpu"
20
  model.to(device)
21
 
22
  # 定义生成函数
23
- def query(data):
24
- inputs = tokenizer(data["inputs"], return_tensors="pt", padding=True, truncation=True).to(device)
 
25
  outputs = model.generate(inputs["input_ids"], attention_mask=inputs["attention_mask"], max_new_tokens=400)
26
  result = tokenizer.decode(outputs[0], skip_special_tokens=True)
27
  return result
28
 
29
- # 测试用例:可以替换为动态输入
30
- data = '''You are an expert in analyzing and summarizing use cases of large language models. You will be provided with a title, description, and content of a news article. Based on the human goals and outcomes, summarize in detail how LLM is used and who the end users are, paying attention to their profession or role. If the human goals or occupation information of end users is not clear, just say you don't know. The title and description are crucial for context. Your summary should integrate both the usage and occupation information clearly in at least 3 sentences.
31
 
32
  Article Title: Unleashing the Power of Generative AI in Content Creation and Marketing
33
  Article Description: Explore the world of generative AI in content creation, from understanding the models to its applications in marketing and content strategy.
@@ -60,10 +52,11 @@ Transformer Models: These models, such as BERT and GPT, have revolutionized NLP
60
  '''
61
 
62
 
63
- # 直接调用生成函数并输出结果
64
- # 使用 Gradio 创建前端界面
65
- output = query(data)
66
 
67
  # 将生成的结果保存到 output.txt 文件
68
  with open("output.txt", "w") as f:
69
- f.write(output)
 
 
 
1
  import torch
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
 
 
 
 
 
 
3
 
4
  # 加载模型和tokenizer
5
+ model_name = "./" # 假设模型文件在根目录
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
7
  model = AutoModelForCausalLM.from_pretrained(model_name)
8
 
9
  tokenizer.add_special_tokens({'pad_token': '[PAD]'})
 
10
  device = "cuda" if torch.cuda.is_available() else "cpu"
11
  model.to(device)
12
 
13
  # 定义生成函数
14
+ def query(inputs):
15
+ # 将输入字符串转化为token并传递给模型
16
+ inputs = tokenizer(inputs, return_tensors="pt", padding=True, truncation=True).to(device)
17
  outputs = model.generate(inputs["input_ids"], attention_mask=inputs["attention_mask"], max_new_tokens=400)
18
  result = tokenizer.decode(outputs[0], skip_special_tokens=True)
19
  return result
20
 
21
+ # 自动化输入
22
+ input_text = '''You are an expert in analyzing and summarizing use cases of large language models. You will be provided with a title, description, and content of a news article. Based on the human goals and outcomes, summarize in detail how LLM is used and who the end users are, paying attention to their profession or role. If the human goals or occupation information of end users is not clear, just say you don't know. The title and description are crucial for context. Your summary should integrate both the usage and occupation information clearly in at least 3 sentences.
23
 
24
  Article Title: Unleashing the Power of Generative AI in Content Creation and Marketing
25
  Article Description: Explore the world of generative AI in content creation, from understanding the models to its applications in marketing and content strategy.
 
52
  '''
53
 
54
 
55
+ # 调用生成函数并获取输出
56
+ output = query(input_text)
 
57
 
58
  # 将生成的结果保存到 output.txt 文件
59
  with open("output.txt", "w") as f:
60
+ f.write(output)
61
+
62
+ print("Generated Summary saved to output.txt")