Spaces:
Sleeping
Sleeping
File size: 2,182 Bytes
7c62a85 24c8493 9877492 24c8493 d4b3cde 24c8493 e6a3b31 24c8493 c36c40a fc9c323 c36c40a 24c8493 e6a3b31 24c8493 e6a3b31 36bc4c0 24c8493 fc9c323 36bc4c0 24c8493 ff14a63 36bc4c0 7c62a85 36bc4c0 7c62a85 d3456d2 c36c40a 24c8493 d3456d2 24c8493 7c62a85 24c8493 d4b3cde 7c62a85 20ce7e8 d3456d2 36bc4c0 |
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 |
import gradio as gr
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# Load GPT-2 for both initial argument and counter-argument
model = GPT2LMHeadModel.from_pretrained("gpt2-medium")
tokenizer = GPT2Tokenizer.from_pretrained("gpt2-medium")
def generate_argument(prompt, max_length=200, temperature=0.7):
try:
inputs = tokenizer.encode(prompt, return_tensors="pt")
outputs = model.generate(
inputs,
max_length=max_length,
temperature=temperature,
num_return_sequences=1,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
return tokenizer.decode(outputs[0], skip_special_tokens=True).replace(prompt, "").strip()
except Exception as e:
return f"An error occurred: {str(e)}"
def generate_initial_argument(query):
prompt = f"Topic: {query}\n\nProvide a logical explanation supporting this topic:\n"
return generate_argument(prompt, max_length=150)
def generate_counter_argument(query, initial_argument):
prompt = f"Topic: {query}\n\nInitial argument: {initial_argument}\n\nProvide a well-reasoned counter-argument:\n"
return generate_argument(prompt, max_length=200, temperature=0.8)
def debate(query):
initial_argument = generate_initial_argument(query)
counter_argument = generate_counter_argument(query, initial_argument)
return initial_argument, counter_argument
# Define the Gradio interface
iface = gr.Interface(
fn=debate,
inputs=gr.Textbox(lines=2, placeholder="Enter your question or topic for debate here..."),
outputs=[
gr.Textbox(label="Initial Argument (GPT-2)"),
gr.Textbox(label="Counter-Argument (GPT-2)")
],
title="Two-Perspective Debate System",
description="Enter a question or topic. GPT-2 will provide an initial argument and a counter-argument.",
examples=[
["Is it good for kids to go to schools, or are they wasting their time?"],
["Should governments prioritize space exploration or addressing climate change?"],
["Is genetic engineering in humans ethical for disease prevention?"]
]
)
# Launch the interface
iface.launch() |