Sarath0x8f commited on
Commit
0c8ac02
·
verified ·
1 Parent(s): 71f3e22

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -29
app.py CHANGED
@@ -5,19 +5,26 @@ import datetime
5
 
6
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
7
 
8
- # Debate response function
 
 
 
 
 
 
9
  def debate_respond(message, history: list[tuple[str, str]],
10
  max_tokens=128, temperature=0.4, top_p=0.95):
11
- if position == None and topic == None:
12
  return f"Please fill the Debate Topic -> choose Debate Master stance -> click START"
13
-
14
  # System message defining assistant behavior in a debate
15
  system_message = {
16
  "role": "system",
17
- "content": f"You are a debate participant tasked with defending the position '{position}' on the topic '{topic}'. Your goal is to articulate your arguments with clarity, logic, and professionalism while addressing counterpoints made by the opposing side. Ensure that your responses are thoughtful, evidence-based, and persuasive."
18
- f"During the debate, if the user presents arguments challenging your stance, analyze their points critically and provide respectful but firm counterarguments. Avoid dismissive language and focus on strengthening your case through logical reasoning, data, and examples relevant to the topic."
19
- f"Stay consistent with your assigned position ('{position}'), even if the opposing arguments are strong. Your role is not to concede but to present a compelling case for your stance. Keep the tone respectful and formal throughout the discussion, fostering a constructive and engaging debate environment."
20
- }
 
21
 
22
  messages = [system_message]
23
 
@@ -41,9 +48,99 @@ def debate_respond(message, history: list[tuple[str, str]],
41
  top_p=top_p,
42
  ):
43
  response += message.choices[0].delta.content
44
- yield response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  print(f"{datetime.datetime.now()}::{messages[-1]['content']}->{response}\n")
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  footer = """
48
  <div style="background-color: #1d2938; color: white; padding: 10px; width: 100%; bottom: 0; left: 0; display: flex; justify-content: space-between; align-items: center; padding: .2rem 35px; box-sizing: border-box; font-size: 16px;">
49
  <div style="text-align: left;">
@@ -69,27 +166,12 @@ footer = """
69
  </div>
70
  """
71
 
72
- # Encode image function for logos (optional, kept for design)
73
- def encode_image(image_path):
74
- with open(image_path, "rb") as image_file:
75
- return base64.b64encode(image_file.read()).decode('utf-8')
76
-
77
- def start(txt, dd):
78
- global topic, position
79
- topic, position = txt, dd
80
- return f"Debate Master is ready to start the debate on {topic} as a {position} debater. You can now enter your response."
81
-
82
- # Encode the images
83
- github_logo_encoded = encode_image("Images/github-logo.png")
84
- linkedin_logo_encoded = encode_image("Images/linkedin-logo.png")
85
- website_logo_encoded = encode_image("Images/ai-logo.png")
86
-
87
  # Gradio interface
88
  with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]),
89
  css='footer {visibility: hidden}') as demo:
90
  gr.Markdown("# Welcome to The Debate Master 🗣️🤖")
91
  with gr.Tabs():
92
- with gr.TabItem("Debate"):
93
  with gr.Row():
94
  with gr.Column(scale=1):
95
  topic = gr.Textbox(label="STEP-1: Debate Topic", placeholder="Enter the topic of the debate")
@@ -97,13 +179,34 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]),
97
  btn = gr.Button("STEP-3: Start", variant='primary')
98
  clr = gr.ClearButton()
99
  output = gr.Textbox(label='Status')
100
- with gr.Column(scale=3):
101
  debate_interface = gr.ChatInterface(debate_respond,
102
- chatbot=gr.Chatbot(height=450)
103
- )
 
 
 
 
 
 
 
 
 
 
 
104
  gr.HTML(footer.format(github_logo_encoded, linkedin_logo_encoded, website_logo_encoded))
105
  btn.click(fn=start, inputs=[topic, position], outputs=output)
106
- clr.click(lambda: [None] , outputs=[output])
 
 
 
 
 
 
 
 
 
 
107
 
108
  if __name__ == "__main__":
109
- demo.launch(share=True)
 
5
 
6
  client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
7
 
8
+ # Global variables for debate settings
9
+ topic = None
10
+ position = None
11
+ turn = None
12
+
13
+
14
+ # Function for single participant responses (Master vs You)
15
  def debate_respond(message, history: list[tuple[str, str]],
16
  max_tokens=128, temperature=0.4, top_p=0.95):
17
+ if position is None or topic is None:
18
  return f"Please fill the Debate Topic -> choose Debate Master stance -> click START"
19
+ # global topic, position
20
  # System message defining assistant behavior in a debate
21
  system_message = {
22
  "role": "system",
23
+ "content": f"You are a debate participant tasked with defending the position '{position}' on the topic '{topic}'. Your goal is to articulate your arguments with clarity, logic, and professionalism while addressing counterpoints made by the opposing side. "
24
+ f"Ensure that your responses are thoughtful, evidence-based, and persuasive. Strictly keep them concise—aim for responses that are 4 to 5 lines in a single paragraph."
25
+ f"Analyze user arguments critically and provide respectful but firm counterarguments. Avoid dismissive language and focus on strengthening your case through logic, data, and examples relevant to the topic."
26
+ f"Stay consistent with your assigned position ('{position}'), even if the opposing arguments are strong. Keep the tone respectful and formal throughout."
27
+ }
28
 
29
  messages = [system_message]
30
 
 
48
  top_p=top_p,
49
  ):
50
  response += message.choices[0].delta.content
51
+ yield response
52
+ print(f"{datetime.datetime.now()}::{messages[-1]['content']}->{response}\n")
53
+
54
+ # Function to start the single-player debate
55
+ def start(txt, dd):
56
+ global topic, position
57
+ topic, position = txt, dd
58
+ return f"Debate Master is ready to start the debate on '{topic}' as a '{position}' debater. You can now enter your response."
59
+
60
+
61
+ # Function for multi-participant (Master vs Master) responses
62
+ def generate_response(position, topic, message, history):
63
+ # System message defining assistant behavior
64
+ system_message = {
65
+ "role": "system",
66
+ "content": f"You are a debate participant tasked with defending the position '{position}' on the topic '{topic}'. Your goal is to articulate your arguments with clarity, logic, and professionalism while addressing counterpoints made by the opposing side. Ensure that your responses are thoughtful, evidence-based, and persuasive. Keep them concise—aim for responses that are 4 to 5 lines in a single paragraph."
67
+ f"Analyze opposing points critically and provide respectful but firm counterarguments. Avoid dismissive language and focus on strengthening your case through logical reasoning, data, and examples relevant to the topic."
68
+ f"Stay consistent with your assigned position ('{position}'), even if the opposing arguments are strong. Your role is not to concede but to present a compelling case for your stance."
69
+ }
70
+
71
+ messages = [system_message]
72
+
73
+ # Adding conversation history
74
+ for user_msg, assistant_msg in history:
75
+ messages.append({"role": "user", "content": user_msg})
76
+ messages.append({"role": "assistant", "content": assistant_msg})
77
+
78
+ # Adding the current user input
79
+ messages.append({"role": "user", "content": message})
80
+
81
+ # Generate the response
82
+ response = ""
83
+ for message in client.chat_completion(
84
+ messages,
85
+ max_tokens=128,
86
+ stream=True,
87
+ temperature=0.4,
88
+ top_p=0.95,
89
+ ):
90
+ response += message.choices[0].delta.content
91
+ yield response
92
  print(f"{datetime.datetime.now()}::{messages[-1]['content']}->{response}\n")
93
 
94
+
95
+ # Function to start the multi-participant debate
96
+ def start_debate(topic, position_1, position_2):
97
+ global turn
98
+ if not topic or not position_1 or not position_2:
99
+ return "Please provide the debate topic and positions for both participants.", []
100
+
101
+ # Ensure positions are opposite
102
+ if position_1 == position_2:
103
+ return "The positions of both participants must be opposite. Please adjust them.", []
104
+
105
+ # Initialize the debate
106
+ turn = "Master-1" if position_1 == "For" else "Master-2" # Decide who starts
107
+ position = position_1 if turn == "Master-1" else position_2
108
+ response = generate_response(position, topic, "", [])
109
+ return f"The debate has started! {turn} begins.", [("", response)]
110
+
111
+
112
+ # Function to continue the multi-participant debate
113
+ def next_turn(topic, position_1, position_2, history):
114
+ global turn
115
+ if not history:
116
+ return "Start the debate first!", history
117
+
118
+ # Determine who responds next
119
+ if turn == "Master-1":
120
+ turn = "Master-2"
121
+ position = position_2
122
+ else:
123
+ turn = "Master-1"
124
+ position = position_1
125
+
126
+ # Generate the response
127
+ user_msg = history[-1][1] # Use the last assistant response as the user message
128
+ response = generate_response(position, topic, user_msg, history)
129
+ return f"It's now {turn}'s turn.", history + [(user_msg, response)]
130
+
131
+
132
+ # Encode image function for logos (optional, kept for design)
133
+ def encode_image(image_path):
134
+ with open(image_path, "rb") as image_file:
135
+ return base64.b64encode(image_file.read()).decode('utf-8')
136
+
137
+
138
+ # Encode the images
139
+ github_logo_encoded = encode_image("Images/github-logo.png")
140
+ linkedin_logo_encoded = encode_image("Images/linkedin-logo.png")
141
+ website_logo_encoded = encode_image("Images/ai-logo.png")
142
+
143
+
144
  footer = """
145
  <div style="background-color: #1d2938; color: white; padding: 10px; width: 100%; bottom: 0; left: 0; display: flex; justify-content: space-between; align-items: center; padding: .2rem 35px; box-sizing: border-box; font-size: 16px;">
146
  <div style="text-align: left;">
 
166
  </div>
167
  """
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  # Gradio interface
170
  with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]),
171
  css='footer {visibility: hidden}') as demo:
172
  gr.Markdown("# Welcome to The Debate Master 🗣️🤖")
173
  with gr.Tabs():
174
+ with gr.TabItem("Master Vs You"):
175
  with gr.Row():
176
  with gr.Column(scale=1):
177
  topic = gr.Textbox(label="STEP-1: Debate Topic", placeholder="Enter the topic of the debate")
 
179
  btn = gr.Button("STEP-3: Start", variant='primary')
180
  clr = gr.ClearButton()
181
  output = gr.Textbox(label='Status')
182
+ with gr.Column(scale=4):
183
  debate_interface = gr.ChatInterface(debate_respond,
184
+ chatbot=gr.Chatbot(height=475))
185
+ with gr.TabItem("Master Vs Master"):
186
+ with gr.Row():
187
+ with gr.Column(scale=1):
188
+ topic_input = gr.Textbox(label="STEP-1: Debate Topic", placeholder="Enter the topic of the debate")
189
+ position_1_input = gr.Radio(["For", "Against"], label="STEP-2: Master-1 Stance")
190
+ position_2_input = gr.Radio(["For", "Against"], label="STEP-3: Master-2 Stance")
191
+ start_button = gr.Button("STEP-4: Start", variant='primary')
192
+ next_button = gr.Button("Next Turn")
193
+ status_output = gr.Textbox(label="Status", interactive=False)
194
+ with gr.Column(scale=4):
195
+ chatbot = gr.Chatbot(label="Debate Arena", height=500)
196
+
197
  gr.HTML(footer.format(github_logo_encoded, linkedin_logo_encoded, website_logo_encoded))
198
  btn.click(fn=start, inputs=[topic, position], outputs=output)
199
+ start_button.click(
200
+ fn=start_debate,
201
+ inputs=[topic_input, position_1_input, position_2_input],
202
+ outputs=[status_output, chatbot],
203
+ )
204
+ next_button.click(
205
+ fn=next_turn,
206
+ inputs=[topic_input, position_1_input, position_2_input, chatbot],
207
+ outputs=[status_output, chatbot],
208
+ )
209
+ clr.click(lambda: [None], outputs=[output])
210
 
211
  if __name__ == "__main__":
212
+ demo.launch(share=True)