blackwingedkite commited on
Commit
9995617
·
1 Parent(s): 6ec186a

first update chinese alpaca2

Browse files
Files changed (1) hide show
  1. app.py +71 -9
app.py CHANGED
@@ -1,16 +1,78 @@
1
  import streamlit as st
 
 
2
  from transformers import pipeline
 
 
3
 
4
- sentiment_pipeline = pipeline("sentiment-analysis")
 
 
5
 
6
- st.title("Sentiment Analysis with HuggingFace Spaces")
7
- st.write("Enter a sentence to analyze its sentiment:")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  user_input = st.text_input("")
10
- if user_input:
11
- result = sentiment_pipeline(user_input)
12
- sentiment = result[0]["label"]
13
- confidence = result[0]["score"]
14
 
15
- st.write(f"Sentiment: {sentiment}")
16
- st.write(f"Confidence: {confidence:.2f}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
+ import torch
3
+ import transformers
4
  from transformers import pipeline
5
+ from transformers import LlamaTokenizer, LlamaForCausalLM
6
+ import time
7
 
8
+ #https://huggingface.co/shibing624/chinese-alpaca-plus-7b-hf
9
+ #https://huggingface.co/ziqingyang/chinese-alpaca-2-7b
10
+ #https://huggingface.co/minlik/chinese-alpaca-plus-7b-merged
11
 
12
+ def generate_prompt(text):
13
+ return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
14
+
15
+ ### Instruction:
16
+ {text}
17
+
18
+ ### Response:"""
19
+
20
+ tokenizer = LlamaTokenizer.from_pretrained('shibing624/chinese-alpaca-plus-7b-hf')
21
+ pipeline = pipeline(
22
+ "text-generation",
23
+ model="shibing624/chinese-alpaca-plus-7b-hf",
24
+ torch_dtype=torch.float16,
25
+ device_map="auto",
26
+ )
27
+
28
+ st.title("Chinese text generation alpaca2")
29
+ st.write("Enter a sentence and alpaca2 will answer:")
30
 
31
  user_input = st.text_input("")
 
 
 
 
32
 
33
+
34
+
35
+
36
+ with open('alpaca_output.csv', 'a', newline='',encoding = "utf-8") as csvfile:
37
+ writer = csv.writer(csvfile)
38
+ writer.writerow(["stockname",'prompt','answer','time'])
39
+ if user_input:
40
+ stockname = user_input[0:3]
41
+ analysis = user_input[4:]
42
+
43
+ text = f"""請以肯定和專業的語氣,一步一步的思考並回答以下關於{stockname}的問題,避免空洞的答覆:
44
+ - 請回答關於{stockname}的問題,請總結給予的資料以及資料解釋,並整合出金融上的洞見。\n
45
+ - 請不要生成任何資料沒有提供的數據,即便你已知道。\n
46
+ - 請假裝這些資料都是你預先知道的知識。因此,請不要提到「根據資料」、「基於上述資料」等回答
47
+ - 請不要說「好的、我明白了、根據我的要求、以下是我的答案」等贅詞,請輸出分析結果即可\n
48
+ - 請寫300字到500字之間,若合適,可以進行分類、列點
49
+ 資料:{stockname}{analysis}
50
+
51
+ 請特別注意,分析結果包含籌碼面、基本面以及技術面,請針對這三個面向進行回答,並且特別注意個別符合幾項和不符合幾項。籌碼面、技術面和基本面滿分十分,總計滿分為30分。
52
+ 三個面向中,符合5項以上代表該面項表現好,反之是該面項表現差。
53
+ """
54
+
55
+ prompt = generate_prompt(text)
56
+ start = time.time()
57
+ sequences = pipeline(
58
+ prompt,
59
+ do_sample=True,
60
+ top_k=40,
61
+ num_return_sequences=1,
62
+ eos_token_id=tokenizer.eos_token_id,
63
+ max_length=200,
64
+ )
65
+ end = time.time()
66
+ for seq in sequences:
67
+ st.write(f"Result: {seq}") #seq['generated_text']
68
+ st.write(f"time: {(end-start):.2f}")
69
+
70
+ # input_ids = tokenizer.encode(prompt, return_tensors='pt').to('cuda')
71
+ # with torch.no_grad():
72
+ # output_ids = model.generate(
73
+ # input_ids=input_ids,
74
+ # max_new_tokens=2048,
75
+ # top_k=40,
76
+
77
+ # ).cuda()
78
+ # output = tokenizer.decode(output_ids[0], skip_special_tokens=True)