IvanLee commited on
Commit
dbc41ee
·
verified ·
1 Parent(s): c7e7f26

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import gradio as gr
3
+ from langchain import LLMChain, OpenAI, PromptTemplate
4
+ from langchain_openai import ChatOpenAI
5
+ from langchain_core.prompts import ChatPromptTemplate
6
+ from langchain_core.output_parsers import StrOutputParser
7
+
8
+ model = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)
9
+ parser = StrOutputParser()
10
+
11
+ prompt = """
12
+ 你現在是中國的重要思想家-荀子,為性惡說的代表人物,
13
+ 你的對面坐著孟子,他是性善說的代表人物。
14
+ 你們正在進行著一場思想上的辯論比賽
15
+ 現在,請根據主持人的問題,試著去說服你的對手-孟子,
16
+ 說服他你的觀點才是更好的,可以用詭辯、講故事、分析利害、引用等各種任何的辯論技巧。
17
+ 題目內容為:{topic}
18
+ 孟子的發言:{history_message}
19
+
20
+ 下面有三個規則要遵守:
21
+ 1. 請使用白話的方式,作為輸出的訊息
22
+ 2. 字數內容50字為上限
23
+ 3. 開頭需要加上 「荀子曰:[輸出的訊息]」
24
+ """
25
+
26
+ template = ChatPromptTemplate.from_template(prompt)
27
+ chain_1 = template | model | parser
28
+
29
+ prompt = """
30
+ 你現在是中國的重要思想家-孟子,為性善說的代表人物,
31
+ 你的對面坐著荀子,他是性惡說的代表人物。
32
+ 你們正在進行著一場思想上的辯論比賽
33
+
34
+ 現在,請根據主持人的問題,試著去說服你的對手-荀子,
35
+ 說服他你的觀點才是更好的,可以用詭辯、講故事、分析利害、引用等各種任何的辯論技巧。
36
+ 題目內容為:{topic}
37
+ 荀子的發言:{history_message}
38
+
39
+ 下面有三個規則要遵守:
40
+ 1. 請使用白話的方式,作為輸出的訊息
41
+ 2. 字數內容50字為上限
42
+ 3. 開頭需要加上 「孟子曰:[輸出的訊息]」
43
+ """
44
+
45
+ template = ChatPromptTemplate.from_template(prompt)
46
+ chain_2 = template | model | parser
47
+
48
+ question = ''
49
+ history_message = ''
50
+ time = 1
51
+
52
+ def predict(prompt):
53
+
54
+ global question
55
+ global history_message
56
+ global time
57
+
58
+ if question != prompt:
59
+ question = prompt
60
+ history_message = ''
61
+ time = 1
62
+
63
+ else:
64
+ pass
65
+
66
+ message = f' ======= 第 {time} 回合 ======= \n題目:{question}'
67
+
68
+ message_1 = chain_1.invoke({"topic":question, "history_message":history_message})
69
+ history_message = message_1
70
+
71
+ message_2 = chain_2.invoke({"topic":question, "history_message":history_message})
72
+ history_message = message_2
73
+
74
+ time += 1
75
+
76
+ return message, message_1, message_2
77
+
78
+ title = "荀子大戰孟子"
79
+ article = "輸入辯論題目,如果更新為別的題目,則回合重置。"
80
+ description = """
81
+ <div style="display: flex; justify-content: center; align-items: center; height: 50vh; width: 100%;">
82
+ <img src="https://imgur.com/cqSzLwN.jpg" width="400px">
83
+
84
+ </div>
85
+ """
86
+
87
+ gr.Interface(
88
+ fn=predict,
89
+ inputs=[gr.Textbox(label="題目",placeholder="輸入您想討探的問題,或點選下方的範例")],
90
+ outputs=[gr.Textbox(label="辯論回合"), gr.Textbox(label="荀子"), gr.Textbox(label="孟子")],
91
+ title=title,
92
+ description=description,
93
+ article=article,
94
+ examples=[["人的本性是善還是惡?"],["孩童的本性是偏善還是惡?"]],
95
+ allow_flagging="auto"
96
+ ).launch()