jmtek commited on
Commit
77ff9da
·
1 Parent(s): fe85308

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -58
app.py CHANGED
@@ -1,66 +1,66 @@
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;">
@@ -107,8 +107,7 @@ with gr.Blocks() as demo:
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()
 
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;">
 
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
+ demo.launch()