File size: 3,103 Bytes
dbc41ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6cb8813
dbc41ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import openai
import gradio as gr
from langchain import LLMChain, OpenAI, PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

model = ChatOpenAI(model="gpt-4o-mini", temperature=0.7)
parser = StrOutputParser()

prompt = """
  你現在是中國的重要思想家-荀子,為性惡說的代表人物,
  你的對面坐著孟子,他是性善說的代表人物。
  你們正在進行著一場思想上的辯論比賽
  現在,請根據主持人的問題,試著去說服你的對手-孟子,
  說服他你的觀點才是更好的,可以用詭辯、講故事、分析利害、引用等各種任何的辯論技巧。
  題目內容為:{topic}
  孟子的發言:{history_message}

  下面有三個規則要遵守:
  1. 請使用白話的方式,作為輸出的訊息
  2. 字數內容50字為上限
  3. 開頭需要加上 「荀子曰:[輸出的訊息]」 
"""

template = ChatPromptTemplate.from_template(prompt)
chain_1 = template | model | parser

prompt = """
  你現在是中國的重要思想家-孟子,為性善說的代表人物,
  你的對面坐著荀子,他是性惡說的代表人物。
  你們正在進行著一場思想上的辯論比賽

  現在,請根據主持人的問題,試著去說服你的對手-荀子,
  說服他你的觀點才是更好的,可以用詭辯、講故事、分析利害、引用等各種任何的辯論技巧。
  題目內容為:{topic}
  荀子的發言:{history_message}

  下面有三個規則要遵守:
  1. 請使用白話的方式,作為輸出的訊息
  2. 字數內容50字為上限
  3. 開頭需要加上 「孟子曰:[輸出的訊息]」
"""

template = ChatPromptTemplate.from_template(prompt)
chain_2 = template | model | parser

question = ''
history_message = ''
time = 1

def predict(prompt):

    global question
    global history_message
    global time

    if question != prompt:
      question = prompt
      history_message = ''
      time = 1

    else:
      pass
    
    message = f' ======= 第 {time} 回合 ======= \n題目:{question}'

    message_1 = chain_1.invoke({"topic":question, "history_message":history_message})
    history_message = message_1

    message_2 = chain_2.invoke({"topic":question, "history_message":history_message})
    history_message = message_2

    time += 1

    return message, message_1, message_2

title = "荀子大戰孟子"
article = "輸入辯論題目,如果更新為別的題目,則回合重置。"
description = """
        <img src="https://imgur.com/cqSzLwN.jpg" width="400px">
"""

gr.Interface(
    fn=predict,
    inputs=[gr.Textbox(label="題目",placeholder="輸入您想討探的問題,或點選下方的範例")], 
    outputs=[gr.Textbox(label="辯論回合"), gr.Textbox(label="荀子"), gr.Textbox(label="孟子")],
    title=title,
    description=description,
    article=article,
    examples=[["人的本性是善還是惡?"],["孩童的本性是偏善還是惡?"]],
    allow_flagging="auto"
).launch()