| | import gradio as gr |
| | import os |
| | import re |
| | import pandas as pd |
| | import json |
| | from ast import literal_eval |
| | from langchain.llms import OpenAI |
| | from langchain.prompts import PromptTemplate |
| | from langchain.chains import LLMChain |
| | from langchain import OpenAI, ConversationChain |
| | llm=OpenAI(openai_api_key=os.environ.get("OPENAI_API_KEY")) |
| |
|
| | def genCompetitorList(market): |
| | promptCompetitors = PromptTemplate( |
| | input_variables=["market"], |
| | template="You are a helpful Blueocean strategy advisor. Return top 3 {market}. Only response single-quote JSON array without reasoning.", |
| | ) |
| | chain = LLMChain(llm=llm, prompt=promptCompetitors) |
| | responseCompetitorsArray = literal_eval(chain.run(market)) |
| | print(responseCompetitorsArray) |
| | return gr.CheckboxGroup.update(choices=responseCompetitorsArray, interactive=True) |
| |
|
| |
|
| | def genFeatureList(market,competitorList): |
| | promptFeatures = PromptTemplate( |
| | input_variables=["competitors"], |
| | template="return 3 benefits customers get or vendors provide, when customers use product and services of vendors {competitors}. Return only nouns, no adjective. Only response single-quote JSON array without reasoning.输出中文", |
| | ) |
| | chain = LLMChain(llm=llm, prompt=promptFeatures) |
| | responseFeaturesArray=literal_eval(chain.run(' '.join(competitorList))) |
| | print(responseFeaturesArray) |
| | return gr.CheckboxGroup.update(choices=responseFeaturesArray, interactive=True) |
| |
|
| | def genRatingsPlot(market,competitorList,featureList): |
| | |
| | promptRatings = PromptTemplate( |
| | input_variables=["features","competitors"], |
| | template="return scoring 1 to 5 customers rate features:{features} for vendors:{competitors}.Only response single-quote JSON array without reasoning, features sequence and vendors sequence should be exactly the same as input, String \"competitor\" is the key cannot be omitted. Example: \"[\"competitor\": \"vendor1\", \"feature1\": 5, \"feature2\": 4 ; \"competitor\": \"vendor2\", \"feature1\": 2, \"feature2\": 3]\". 文字输出中文,标点符号用英文", |
| | ) |
| | chain = LLMChain(llm=llm, prompt=promptRatings) |
| | responseRatings = chain.run({"features":' '.join(featureList),"competitors":' '.join(competitorList)}) |
| | print(responseRatings.replace("'","\"")) |
| | data = json.loads(responseRatings.replace("'","\"")) |
| | df = pd.DataFrame(data) |
| | print(df) |
| | return gr.LinePlot.update( |
| | value=df, |
| | x="competitor", |
| | y=df.columns[1], |
| | x_title="竞品", |
| | y_title="特性评分", |
| | y_lim=[1,5], |
| | color_legend_position="right", |
| | width=500, |
| | height=300 |
| | ) |
| |
|
| | with gr.Blocks() as demo: |
| |
|
| | market = gr.Textbox(label="细分市场:如大理附近小镇") |
| | btn1 = gr.Button(value="获取竞品") |
| | competitorList = gr.CheckboxGroup(label="竞品列表") |
| | btn1.click(genCompetitorList, inputs=[market], outputs=[competitorList]) |
| | |
| | |
| | btn2 = gr.Button(value="获取特性") |
| | featureList = gr.CheckboxGroup(label="特性列表") |
| | btn2.click(genFeatureList, inputs=[market,competitorList], outputs=[featureList]) |
| | |
| | |
| | btn3 = gr.Button(value="生成战略布局图") |
| | plot = gr.LinePlot(title="战略布局图") |
| | btn3.click(genRatingsPlot,inputs=[market,competitorList,featureList],outputs=[plot]) |
| |
|
| | demo.launch() |