import gradio as gr # from langchain.llms import OpenAI # from langchain.llms.fake import FakeListLLM # from langchain import PromptTemplate, FewShotPromptTemplate # from langchain.utilities import TextRequestsWrapper # import json # def generate_prompt(embedding_url, extra_prompt, count): # output = TextRequestsWrapper().get(embedding_url) # result = json.loads(output) # example_formatter_template = "标题: {sentence}\n热度: {hot_level}" # example_prompt = PromptTemplate( # input_variables=["sentence", "hot_level"], # template=example_formatter_template, # ) # few_shot_prompt = FewShotPromptTemplate( # examples=result["data"], # example_prompt=example_prompt, # prefix="你是一个短视频方专家,你的职责是为短视频的制作进行选题。\n以下是某视频站点的最新热词\n\n", # # The suffix is some text that goes after the examples in the prompt. # # Usually, this is where the user input will go # suffix="{extra_prompt}\n\n请根据以上内容草拟{count}个吸引人的短视频的标题:", # # The input variables are the variables that the overall prompt expects. # input_variables=["extra_prompt", "count"], # # The example_separator is the string we will use to join the prefix, examples, and suffix together with. # example_separator="\n\n", # ) # # We can now generate a prompt using the `format` method. # # print(few_shot_prompt.format(count="5")) # return few_shot_prompt.format(extra_prompt=extra_prompt, count=count) # def generate_topics( # openai_api_key: str, # embedding_url: str, # extra_prompt: str, # count: int, # repeat: int # ): # if openai_api_key.strip() == "": # return "请输入OPENAI API KEY" # llm = OpenAI(temperature=.7, openai_api_key=openai_api_key) # prompt = generate_prompt(embedding_url, extra_prompt, count) # print(prompt) # llm_result = llm.generate([prompt]*repeat) # answer = "" if repeat == 1 else f"以下是合并的{str(repeat)}次选题结果:\n\n" # for i in range(len(llm_result.generations)): # answer += f"{'下一个:' if i > 0 else ''}" + llm_result.generations[i][0].text + "\n\n" # print(answer) # token_usage = llm_result.llm_output["token_usage"]["total_tokens"] if llm_result.llm_output else 0 # answer += f"##总共消耗Token数:{str(token_usage)}" # return answer with gr.Blocks() as demo: gr.HTML("""

Generate Topics & Scripts by LLM

""") topics_output = gr.Textbox(label='选题结果') # extra_req = gr.Textbox(label = 'Type in your query args for your douyin retriever api(optional)', placeholder = '') # 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='') # api_path = gr.Textbox(label = 'Type in the api path (ref to the spec above, like "/public/openai/v0/products")', placeholder = '') embedding_url = gr.Textbox(label = '热门内容获取API的URL') # examples = gr.Examples(examples=["https://huggingface.co/spaces/JStudio/Video_Topics_Scripts_LLMGen/raw/main/douyin_1.txt"], # inputs=[embedding_url]) extra_prompt = gr.Textbox(label = '追加的一些Prompt(可选)') with gr.Row(): openai_api_key = gr.Textbox(type='password', label="输入你的OpenAI API key") with gr.Row(): count = gr.Slider( label="每次生成几个选题", value=5, minimum=0, maximum=10, step=1 ) repeat = gr.Slider( label="让ChatGPT重试几次", value=1, minimum=0, maximum=10, step=1 ) # fakeLLM = gr.CheckboxGroup(choices=["是"], value=[], label="调用FakeLLM") btn = gr.Button("生成选题") # output = gr.Textbox(label="Topics Generating Output") # topic = gr.Textbox(label='需要生成视频脚本的选题') # keyword = gr.Textbox(label='内容相关的关键字') # btn2 = gr.Button("生成视频创作脚本") # output2 = gr.Textbox(label="Scripts") #btn.click(generate_topics, [openai_api_key, embedding_url, extra_prompt, count, repeat], [topics_output]) # btn2.click(chat.generateScripts, [], [chatbot]) demo.launch()