Spaces:
Sleeping
Sleeping
Commit
•
8c6710d
0
Parent(s):
Duplicate from xl2533/WhatToAsk
Browse files- .gitattributes +34 -0
- README.md +13 -0
- __pycache__/prompt.cpython-38.pyc +0 -0
- __pycache__/qa.cpython-38.pyc +0 -0
- app.py +30 -0
- prompt.py +17 -0
- qa.py +40 -0
- requirements.txt +6 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: WhatToAsk
|
3 |
+
emoji: ⚡
|
4 |
+
colorFrom: pink
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.24.1
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: xl2533/WhatToAsk
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
__pycache__/prompt.cpython-38.pyc
ADDED
Binary file (731 Bytes). View file
|
|
__pycache__/qa.cpython-38.pyc
ADDED
Binary file (1.27 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*-coding:utf-8 -*-
|
2 |
+
|
3 |
+
import os
|
4 |
+
import gradio as gr
|
5 |
+
from qa import *
|
6 |
+
|
7 |
+
with gr.Blocks(title="QA Generation", theme=gr.themes.Glass()) as demo:
|
8 |
+
openai_key = gr.Textbox(type='password', label='输入 API key')
|
9 |
+
gr.Markdown("## Question Answer Generation")
|
10 |
+
with gr.Row():
|
11 |
+
input = gr.Textbox(label='上下文')
|
12 |
+
qa = gr.JSON(label='问题&回答对')
|
13 |
+
gen = gr.Button(value='generate')
|
14 |
+
|
15 |
+
gr.Markdown('## Answer Generation')
|
16 |
+
with gr.Row():
|
17 |
+
context = gr.Textbox(label='上下文')
|
18 |
+
question = gr.Textbox(label ='问题')
|
19 |
+
answer = gr.Textbox(label='答案')
|
20 |
+
ask = gr.Button(value='ask')
|
21 |
+
|
22 |
+
|
23 |
+
## Callback
|
24 |
+
gen.click(get_qa, [input, openai_key], [qa])
|
25 |
+
|
26 |
+
ask.click(get_answer, [context, question, openai_key], [answer])
|
27 |
+
|
28 |
+
|
29 |
+
# launch
|
30 |
+
demo.queue().launch(debug=True)
|
prompt.py
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*-coding:utf-8 -*-
|
2 |
+
|
3 |
+
QA_gen_user_prompt = """你是金融领域负责出题的老师。给一段文本,你需要想出合适的问题/答案对,包括但不限于经济,政策,金融市场,公司理财,基金等相关领域。返回如下json格式
|
4 |
+
{{"问题":$问题,"回答":$回答}}
|
5 |
+
文本: {text}
|
6 |
+
问题/回答对:
|
7 |
+
"""
|
8 |
+
|
9 |
+
QA_gen_sys_prompt = ""
|
10 |
+
|
11 |
+
QA_answer_sys_prompt = ""
|
12 |
+
|
13 |
+
QA_answer_user_prompt = """你是一个金融问答系统,只使用已有上下文进行问题回答,当上下文无法回答对应问题时,直接回复'无法回答'。
|
14 |
+
上下文:{text}
|
15 |
+
问题:{question}
|
16 |
+
回答:
|
17 |
+
"""
|
qa.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*-coding:utf-8 -*-
|
2 |
+
import json
|
3 |
+
from langchain.prompts.chat import (
|
4 |
+
ChatPromptTemplate,
|
5 |
+
SystemMessagePromptTemplate,
|
6 |
+
HumanMessagePromptTemplate,
|
7 |
+
)
|
8 |
+
from langchain.chains import LLMChain
|
9 |
+
from prompt import *
|
10 |
+
from langchain.chat_models import ChatOpenAI
|
11 |
+
|
12 |
+
|
13 |
+
def get_qa(text, openai_key):
|
14 |
+
llm = ChatOpenAI(openai_api_key=openai_key, max_tokens=2000, temperature=0.8)
|
15 |
+
prompt = ChatPromptTemplate.from_messages(
|
16 |
+
[
|
17 |
+
SystemMessagePromptTemplate.from_template(QA_gen_sys_prompt),
|
18 |
+
HumanMessagePromptTemplate.from_template(QA_gen_user_prompt),
|
19 |
+
]
|
20 |
+
)
|
21 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
22 |
+
print('Generating Question from template')
|
23 |
+
qa = chain({'text': text})
|
24 |
+
result = json.loads(qa['text'])
|
25 |
+
return result
|
26 |
+
|
27 |
+
|
28 |
+
def get_answer(context, question, openai_key):
|
29 |
+
llm = ChatOpenAI(openai_api_key=openai_key, max_tokens=2000, temperature=0.8)
|
30 |
+
prompt = ChatPromptTemplate.from_messages(
|
31 |
+
[
|
32 |
+
SystemMessagePromptTemplate.from_template(QA_answer_sys_prompt),
|
33 |
+
HumanMessagePromptTemplate.from_template(QA_answer_user_prompt),
|
34 |
+
]
|
35 |
+
)
|
36 |
+
chain = LLMChain(llm=llm, prompt=prompt)
|
37 |
+
print('Generating Question from template')
|
38 |
+
answer = chain({'text': context, 'question': question})
|
39 |
+
answer = answer['text']
|
40 |
+
return answer
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai==0.27.2
|
2 |
+
gradio==3.21.0
|
3 |
+
langchain==0.0.113
|
4 |
+
tiktoken==0.1.2
|
5 |
+
sentence-transformers==2.2.2
|
6 |
+
rouge_score
|