jmtek commited on
Commit
aa42290
·
1 Parent(s): 5021ac4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -0
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain.llms import OpenAI
3
+ from langchain.llms.fake import FakeListLLM
4
+
5
+ from langchain import PromptTemplate, FewShotPromptTemplate
6
+ from langchain.utilities import TextRequestsWrapper
7
+ import json
8
+
9
+ def generate_prompt(embedding_url, extra_prompt, count):
10
+
11
+ output = TextRequestsWrapper().get(embedding_url)
12
+ result = json.loads(output)
13
+
14
+ example_formatter_template = "标题: {sentence}\n热度: {hot_level}"
15
+ example_prompt = PromptTemplate(
16
+ input_variables=["sentence", "hot_level"],
17
+ template=example_formatter_template,
18
+ )
19
+
20
+ few_shot_prompt = FewShotPromptTemplate(
21
+ examples=result["data"],
22
+ example_prompt=example_prompt,
23
+ prefix="你是一个短视频方专家,你的职责是为短视频的制作进行选题。\n以下是某视频站点的最新热词\n\n",
24
+ # The suffix is some text that goes after the examples in the prompt.
25
+ # Usually, this is where the user input will go
26
+ suffix="{extra_prompt}\n\n请根据以上内容草拟{count}个吸引人的短视频的标题:",
27
+ # The input variables are the variables that the overall prompt expects.
28
+ input_variables=["extra_prompt", "count"],
29
+ # The example_separator is the string we will use to join the prefix, examples, and suffix together with.
30
+ example_separator="\n\n",
31
+ )
32
+
33
+ # We can now generate a prompt using the `format` method.
34
+ # print(few_shot_prompt.format(count="5"))
35
+ return few_shot_prompt.format(extra_prompt=extra_prompt, count=count)
36
+
37
+ def generate_topics(
38
+ openai_api_key: str,
39
+ embedding_url: str,
40
+ extra_prompt: str,
41
+ count: int,
42
+ repeat: int
43
+ ):
44
+ if openai_api_key.strip() == "":
45
+ return "请输入OPENAI API KEY"
46
+
47
+ llm = OpenAI(temperature=.7, openai_api_key=openai_api_key)
48
+
49
+ prompt = generate_prompt(embedding_url, extra_prompt, count)
50
+ print(prompt)
51
+
52
+ llm_result = llm.generate([prompt]*repeat)
53
+
54
+ answer = "" if repeat == 1 else f"以下是合并的{str(repeat)}次选题结果:\n\n"
55
+ for i in range(len(llm_result.generations)):
56
+ answer += f"{'下一个:' if i > 0 else ''}" + llm_result.generations[i][0].text + "\n\n"
57
+
58
+ print(answer)
59
+
60
+ token_usage = llm_result.llm_output["token_usage"]["total_tokens"] if llm_result.llm_output else 0
61
+ answer += f"##总共消耗Token数:{str(token_usage)}"
62
+
63
+ return answer
64
+
65
+ with gr.Blocks() as demo:
66
+ gr.HTML("""<div style="text-align: center; max-width: 700px; margin: 0 auto;">
67
+ <div
68
+ style="
69
+ display: inline-flex;
70
+ align-items: center;
71
+ gap: 0.8rem;
72
+ font-size: 1.75rem;
73
+ "
74
+ >
75
+ <h1 style="font-weight: 900; margin-bottom: 7px; margin-top: 5px;">
76
+ Generate Topics & Scripts by LLM
77
+ </h1>
78
+ </div>
79
+ </div>""")
80
+
81
+ topics_output = gr.Textbox(label='选题结果')
82
+
83
+ # extra_req = gr.Textbox(label = 'Type in your query args for your douyin retriever api(optional)', placeholder = '')
84
+ # api_spec = gr.Textbox(label = 'Type in your douyin retriever api spec (ref to https://www.klarna.com/us/shopping/public/openai/v0/api-docs/)', placeholder='')
85
+ # api_path = gr.Textbox(label = 'Type in the api path (ref to the spec above, like "/public/openai/v0/products")', placeholder = '')
86
+ embedding_url = gr.Textbox(label = '热门内容获取API的URL')
87
+ # examples = gr.Examples(examples=["https://huggingface.co/spaces/JStudio/Video_Topics_Scripts_LLMGen/raw/main/douyin_1.txt"],
88
+ # inputs=[embedding_url])
89
+ extra_prompt = gr.Textbox(label = '追加的一些Prompt(可选)')
90
+ with gr.Row():
91
+ openai_api_key = gr.Textbox(type='password', label="输入你的OpenAI API key")
92
+
93
+ with gr.Row():
94
+ count = gr.Slider(
95
+ label="每次生成几个选题", value=5, minimum=0, maximum=10, step=1
96
+ )
97
+ repeat = gr.Slider(
98
+ label="让ChatGPT重试几次", value=1, minimum=0, maximum=10, step=1
99
+ )
100
+ # fakeLLM = gr.CheckboxGroup(choices=["是"], value=[], label="调用FakeLLM")
101
+
102
+ btn = gr.Button("生成选题")
103
+ # output = gr.Textbox(label="Topics Generating Output")
104
+
105
+ # topic = gr.Textbox(label='需要生成视频脚本的选题')
106
+ # keyword = gr.Textbox(label='内容相关的关键字')
107
+ # btn2 = gr.Button("生成视频创作脚本")
108
+ # output2 = gr.Textbox(label="Scripts")
109
+
110
+ btn.click(generate_topics, [openai_api_key, embedding_url, extra_prompt, count, repeat], [topics_output])
111
+ # btn2.click(chat.generateScripts, [], [chatbot])
112
+
113
+ if __name__ == "__main__":
114
+ demo.launch()