Debate_Master / app.py
Sarath0x8f's picture
Update app.py
44b5ef3 verified
raw
history blame
5.68 kB
from huggingface_hub import InferenceClient
import gradio as gr
import base64
import datetime
client = InferenceClient("meta-llama/Meta-Llama-3-8B-Instruct")
# Debate response function
def debate_respond(message, history: list[tuple[str, str]],
max_tokens=128, temperature=0.4, top_p=0.95):
if position == None and topic == None:
return f"Please fill the Debate Topic -> choose Debate Master stance -> click START"
# System message defining assistant behavior in a debate
system_message = {
"role": "system",
"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."
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."
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."
}
messages = [system_message]
# Adding conversation history
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
# Adding the current user input
messages.append({"role": "user", "content": message})
# Generating the response
response = ""
for message in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
response += message.choices[0].delta.content
yield response
print(f"{datetime.datetime.now()}::{messages[-1]['content']}->{response}\n")
footer = """
<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;">
<div style="text-align: left;">
<p style="margin: 0;">&copy; 2024 </p>
</div>
<div style="text-align: center; flex-grow: 1;">
<p style="margin: 0;"> This website is made with ❤ by SARATH CHANDRA</p>
</div>
<div class="social-links" style="display: flex; gap: 20px; justify-content: flex-end; align-items: center;">
<a href="https://github.com/21bq1a4210" target="_blank" style="text-align: center;">
<img src="data:image/png;base64,{}" alt="GitHub" width="40" height="40" style="display: block; margin: 0 auto;">
<span style="font-size: 14px;">GitHub</span>
</a>
<a href="https://www.linkedin.com/in/sarath-chandra-bandreddi-07393b1aa/" target="_blank" style="text-align: center;">
<img src="data:image/png;base64,{}" alt="LinkedIn" width="40" height="40" style="display: block; margin: 0 auto;">
<span style="font-size: 14px;">LinkedIn</span>
</a>
<a href="https://21bq1a4210.github.io/MyPortfolio-/" target="_blank" style="text-align: center;">
<img src="data:image/png;base64,{}" alt="Portfolio" width="40" height="40" style="display: block; margin-right: 40px;">
<span style="font-size: 14px;">Portfolio</span>
</a>
</div>
</div>
"""
# Encode image function for logos (optional, kept for design)
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def start(txt, dd):
global topic, position
topic, position = txt, dd
return f"Debate Master is ready to start the debate on {topic} as a {position} debater. You can now enter your response."
# Encode the images
github_logo_encoded = encode_image("Images/github-logo.png")
linkedin_logo_encoded = encode_image("Images/linkedin-logo.png")
website_logo_encoded = encode_image("Images/ai-logo.png")
# Gradio interface
with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]),
css='footer {visibility: hidden}') as demo:
gr.Markdown("# Welcome to The Debate Master 🗣️🤖")
with gr.Tabs():
with gr.TabItem("Debate"):
with gr.Row():
with gr.Column(scale=1):
topic = gr.Textbox(label="STEP-1: Debate Topic", placeholder="Enter the topic of the debate")
position = gr.Radio(["For", "Against"], label="STEP-2: Debate Master stance", scale=1)
btn = gr.Button("STEP-3: Start", variant='primary')
clr = gr.ClearButton()
output = gr.Textbox(label='Status')
with gr.Column(scale=3):
debate_interface = gr.ChatInterface(debate_respond,
chatbot=gr.Chatbot(height=450)
)
gr.HTML(footer.format(github_logo_encoded, linkedin_logo_encoded, website_logo_encoded))
btn.click(fn=start, inputs=[topic, position], outputs=output)
clr.click(lambda: [None] , outputs=[output])
if __name__ == "__main__":
demo.launch(share=True)