File size: 2,698 Bytes
a2d40f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import json
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

# Hugging Face Inference API details
HF_API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.3"

import os
HF_API_KEY = os.getenv("HF_API_KEY")  # Read from environment variable


HEADERS = {"Authorization": f"Bearer {HF_API_KEY}"}

# Function to call Hugging Face API
def query_hf_api(prompt):
    payload = {"inputs": prompt}
    response = requests.post(HF_API_URL, headers=HEADERS, json=payload)
    if response.status_code == 200:
        return response.json()[0]["generated_text"]
    else:
        return f"Error: {response.status_code} - {response.text}"

# Debate function
def conduct_debate(topic):
    # AI Assistant 1's response
    prompt_1 = f"The topic of debate is: {topic}. Present your argument in favor."
    response_1 = query_hf_api(prompt_1)

    # AI Assistant 2's response
    prompt_2 = f"The topic of debate is: {topic}. Present your argument against."
    response_2 = query_hf_api(prompt_2)

    # Judge's conclusion
    prompt_judge = f"The debate topic was: {topic}. One argument in favor: {response_1}. One argument against: {response_2}. As a judge, analyze the arguments and provide a fair conclusion."
    conclusion = query_hf_api(prompt_judge)

    return response_1, response_2, conclusion

# Function to generate a PDF
def generate_pdf(topic, response_1, response_2, conclusion):
    pdf_filename = "debate_result.pdf"
    
    c = canvas.Canvas(pdf_filename, pagesize=letter)
    c.drawString(100, 750, f"Debate Topic: {topic}")
    c.drawString(100, 730, "Argument in Favor:")
    c.drawString(100, 710, response_1[:1000])  # Limiting text for PDF readability
    c.drawString(100, 690, "Argument Against:")
    c.drawString(100, 670, response_2[:1000])
    c.drawString(100, 650, "Judge's Conclusion:")
    c.drawString(100, 630, conclusion[:1000])
    c.save()

    return pdf_filename

# Gradio UI
def debate_interface(topic):
    response_1, response_2, conclusion = conduct_debate(topic)
    pdf_file = generate_pdf(topic, response_1, response_2, conclusion)
    
    return response_1, response_2, conclusion, pdf_file

iface = gr.Interface(
    fn=debate_interface,
    inputs=gr.Textbox(label="Enter Debate Topic"),
    outputs=[
        gr.Textbox(label="Argument in Favor"),
        gr.Textbox(label="Argument Against"),
        gr.Textbox(label="Judge's Conclusion"),
        gr.File(label="Download Debate PDF")
    ],
    title="AI Debate Platform",
    description="Enter a debate topic and let two AI assistants argue, with a third AI model acting as a judge."
)

iface.launch()