Sarath0x8f commited on
Commit
1699fe3
·
verified ·
1 Parent(s): b1d6fbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -49
app.py CHANGED
@@ -3,7 +3,9 @@ import gradio as gr
3
  import base64
4
  import datetime
5
 
6
- client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
 
 
7
 
8
  # Global variables for debate settings
9
  topic = None
@@ -20,10 +22,10 @@ def debate_respond(message, history: list[tuple[str, str]],
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 only in a single paragraph i.e 128 tokens only."
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]
@@ -40,7 +42,7 @@ def debate_respond(message, history: list[tuple[str, str]],
40
 
41
  # Generating the response
42
  response = ""
43
- for message_chunk in client.chat_completion(
44
  messages,
45
  max_tokens=max_tokens,
46
  stream=True,
@@ -59,41 +61,52 @@ def start(txt, dd):
59
  return f"Debate Master is ready to start the debate on '{topic}' as a '{position}' debater. You can now enter your response."
60
 
61
 
62
- # Function for multi-participant (Master vs Master) responses
63
- def generate_response(position, topic, message, history):
64
- # System message defining assistant behavior
65
  system_message = {
66
  "role": "system",
67
- "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."
68
- 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."
69
- 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."
70
  }
71
-
72
  messages = [system_message]
73
 
74
  # Adding conversation history
75
- for user_msg, assistant_msg in history:
76
- messages.append({"role": "user", "content": user_msg})
77
- messages.append({"role": "assistant", "content": assistant_msg})
 
 
78
 
79
  # Adding the current user input
80
  messages.append({"role": "user", "content": message})
81
 
82
- # Generate the response
83
  response = ""
84
- for message_chunk in client.chat_completion(
85
- messages,
86
- max_tokens=128,
87
- stream=True,
88
- temperature=0.4,
89
- top_p=0.95,
90
- ):
91
  response += message_chunk.choices[0].delta.content
 
 
92
 
93
- return response
94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
- # Function to start the multi-participant debate
97
  def start_debate(topic, position_1, position_2):
98
  global turn
99
  if not topic or not position_1 or not position_2:
@@ -103,31 +116,23 @@ def start_debate(topic, position_1, position_2):
103
  if position_1 == position_2:
104
  return "The positions of both participants must be opposite. Please adjust them.", []
105
 
106
- # Initialize the debate
107
- turn = "Master-1" if position_1 == "For" else "Master-2" # Decide who starts
108
- position = position_1 if turn == "Master-1" else position_2
109
- response = generate_response(position, topic, "", [])
110
- return f"The debate has started! {turn} begins.", [("", response)]
111
-
112
 
113
- # Function to continue the multi-participant debate
114
- def next_turn(topic, position_1, position_2, history):
115
  global turn
116
- if not history:
117
- return "Start the debate first!", history
118
-
119
- # Determine who responds next
120
- if turn == "Master-1":
121
  turn = "Master-2"
122
- position = position_2
123
  else:
124
  turn = "Master-1"
125
- position = position_1
126
 
127
- # Generate the response
128
- user_msg = history[-1][1] # Use the last assistant response as the user message
129
- response = generate_response(position, topic, user_msg, history)
130
- return f"It's now {turn}'s turn.", history + [(user_msg, response)]
131
 
132
 
133
  # Encode image function for logos (optional, kept for design)
@@ -181,8 +186,7 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]),
181
  clr = gr.ClearButton()
182
  output = gr.Textbox(label='Status')
183
  with gr.Column(scale=4):
184
- debate_interface = gr.ChatInterface(debate_respond,
185
- chatbot=gr.Chatbot(height=475))
186
  with gr.TabItem("Master Vs Master"):
187
  with gr.Row():
188
  with gr.Column(scale=1):
@@ -192,8 +196,10 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]),
192
  start_button = gr.Button("STEP-4: Start", variant='primary')
193
  next_button = gr.Button("Next Turn")
194
  status_output = gr.Textbox(label="Status", interactive=False)
195
- with gr.Column(scale=4):
196
  chatbot = gr.Chatbot(label="Debate Arena", height=500)
 
 
197
 
198
  gr.HTML(footer.format(github_logo_encoded, linkedin_logo_encoded, website_logo_encoded))
199
  btn.click(fn=start, inputs=[topic, position], outputs=output)
@@ -210,4 +216,4 @@ with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]),
210
  clr.click(lambda: [None], outputs=[output])
211
 
212
  if __name__ == "__main__":
213
- demo.launch(share=True)
 
3
  import base64
4
  import datetime
5
 
6
+ Master1 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
7
+ Master2 = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
8
+ dictionary = InferenceClient("tiiuae/falcon-7b-instruct")
9
 
10
  # Global variables for debate settings
11
  topic = None
 
22
  # System message defining assistant behavior in a debate
23
  system_message = {
24
  "role": "system",
25
+ "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."
26
+ 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."
27
+ 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."
28
+ 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."
29
  }
30
 
31
  messages = [system_message]
 
42
 
43
  # Generating the response
44
  response = ""
45
+ for message_chunk in Master1.chat_completion(
46
  messages,
47
  max_tokens=max_tokens,
48
  stream=True,
 
61
  return f"Debate Master is ready to start the debate on '{topic}' as a '{position}' debater. You can now enter your response."
62
 
63
 
64
+ # Dictionary definition/clarification feature
65
+ def explain_word(message, history: list[tuple[str, str]],max_tokens=128, temperature=0.4, top_p=0.95):
 
66
  system_message = {
67
  "role": "system",
68
+ "content": "You are a helpful assistant providing concise definitions and explanations for words or phrases."
 
 
69
  }
 
70
  messages = [system_message]
71
 
72
  # Adding conversation history
73
+ for val in history:
74
+ if val[0]:
75
+ messages.append({"role": "user", "content": val[0]})
76
+ if val[1]:
77
+ messages.append({"role": "assistant", "content": val[1]})
78
 
79
  # Adding the current user input
80
  messages.append({"role": "user", "content": message})
81
 
 
82
  response = ""
83
+ for message_chunk in dictionary.chat_completion(
84
+ messages, max_tokens=64, stream=True, temperature=0.3, top_p=0.9):
 
 
 
 
 
85
  response += message_chunk.choices[0].delta.content
86
+ yield response
87
+ print(f"{datetime.datetime.now()}::{messages[-1]['content']}->{response}\n")
88
 
 
89
 
90
+ def generate_response(llm, position, topic, message):
91
+ system_message = {
92
+ "role": "system",
93
+ "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."
94
+ 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."
95
+ 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."
96
+ 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."
97
+ }
98
+
99
+ messages = [system_message]
100
+ messages.append({"role": "user", "content": message})
101
+
102
+ response = ""
103
+ for message_chunk in llm.chat_completion(
104
+ messages, max_tokens=128, stream=True, temperature=0.4, top_p=0.95):
105
+ response += message_chunk.choices[0].delta.content
106
+
107
+ # Return the complete response as a string
108
+ return response
109
 
 
110
  def start_debate(topic, position_1, position_2):
111
  global turn
112
  if not topic or not position_1 or not position_2:
 
116
  if position_1 == position_2:
117
  return "The positions of both participants must be opposite. Please adjust them.", []
118
 
119
+ turn = "Master-1"
120
+ initial_message = "Opening Statement"
121
+ response = generate_response(Master1, position_1, topic, initial_message)
122
+ return f"The debate has started! {turn} begins.", [(initial_message, response)]
 
 
123
 
124
+ # Continue debate
125
+ def next_turn(topic, position_1, position_2, current_turn):
126
  global turn
127
+ if current_turn == "Master-1":
 
 
 
 
128
  turn = "Master-2"
129
+ llm, position = Master2, position_2
130
  else:
131
  turn = "Master-1"
132
+ llm, position = Master1, position_1
133
 
134
+ response = generate_response(llm, position, topic, "Your turn to respond.")
135
+ return f"It's now {turn}'s turn.", [("", response)]
 
 
136
 
137
 
138
  # Encode image function for logos (optional, kept for design)
 
186
  clr = gr.ClearButton()
187
  output = gr.Textbox(label='Status')
188
  with gr.Column(scale=4):
189
+ debate_interface = gr.ChatInterface(debate_respond, chatbot=gr.Chatbot(height=475, label="Debate Arena"))
 
190
  with gr.TabItem("Master Vs Master"):
191
  with gr.Row():
192
  with gr.Column(scale=1):
 
196
  start_button = gr.Button("STEP-4: Start", variant='primary')
197
  next_button = gr.Button("Next Turn")
198
  status_output = gr.Textbox(label="Status", interactive=False)
199
+ with gr.Column(scale=2):
200
  chatbot = gr.Chatbot(label="Debate Arena", height=500)
201
+ with gr.Column(scale=1):
202
+ dictionary_search_interface = gr.ChatInterface(explain_word, chatbot=gr.Chatbot(height=450, label="Define word"))
203
 
204
  gr.HTML(footer.format(github_logo_encoded, linkedin_logo_encoded, website_logo_encoded))
205
  btn.click(fn=start, inputs=[topic, position], outputs=output)
 
216
  clr.click(lambda: [None], outputs=[output])
217
 
218
  if __name__ == "__main__":
219
+ demo.launch(share=True)