Upload 2 files
Browse files- app.py +129 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import tempfile
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from groq import Groq
|
| 7 |
+
from gtts import gTTS
|
| 8 |
+
import pyglet
|
| 9 |
+
|
| 10 |
+
# ---------------------------
|
| 11 |
+
# π Load API key from .env
|
| 12 |
+
# ---------------------------
|
| 13 |
+
load_dotenv()
|
| 14 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 15 |
+
if not GROQ_API_KEY:
|
| 16 |
+
raise ValueError("β GROQ_API_KEY not found in .env. Add it like: GROQ_API_KEY=your_key_here")
|
| 17 |
+
|
| 18 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 19 |
+
|
| 20 |
+
# ---------------------------
|
| 21 |
+
# π Voice output (optional)
|
| 22 |
+
# ---------------------------
|
| 23 |
+
def speak_text(text, lang="en"):
|
| 24 |
+
try:
|
| 25 |
+
tts = gTTS(text=text, lang=lang)
|
| 26 |
+
filename = os.path.join(tempfile.gettempdir(), f"{uuid.uuid4()}.mp3")
|
| 27 |
+
tts.save(filename)
|
| 28 |
+
music = pyglet.media.load(filename, streaming=False)
|
| 29 |
+
music.play()
|
| 30 |
+
pyglet.app.run()
|
| 31 |
+
os.remove(filename)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"[TTS Error] {e}")
|
| 34 |
+
|
| 35 |
+
# ---------------------------
|
| 36 |
+
# π§ LLaMA-3 Role Response
|
| 37 |
+
# ---------------------------
|
| 38 |
+
def query_llama(role: str, topic: str, pro: str = "", con: str = "") -> str:
|
| 39 |
+
print(f"[Groq] Calling LLaMA for role: {role}")
|
| 40 |
+
|
| 41 |
+
if role == "pro":
|
| 42 |
+
prompt = f"You are a skilled debater arguing in favor of:\n'{topic}'.\nPresent 3 strong logical points."
|
| 43 |
+
elif role == "con":
|
| 44 |
+
prompt = f"You are a skilled debater arguing against:\n'{topic}'.\nPresent 3 strong logical points."
|
| 45 |
+
elif role == "moderator":
|
| 46 |
+
prompt = (
|
| 47 |
+
f"Topic: {topic}\n\n"
|
| 48 |
+
f"β
Pro Argument:\n{pro}\n\n"
|
| 49 |
+
f"β Con Argument:\n{con}\n\n"
|
| 50 |
+
f"As a neutral judge, rate both arguments (1β10) based on logic, clarity, and evidence. Justify your score clearly."
|
| 51 |
+
)
|
| 52 |
+
else:
|
| 53 |
+
raise ValueError("Invalid role!")
|
| 54 |
+
|
| 55 |
+
try:
|
| 56 |
+
response = client.chat.completions.create(
|
| 57 |
+
model="llama3-70b-8192",
|
| 58 |
+
messages=[{"role": "user", "content": prompt}]
|
| 59 |
+
)
|
| 60 |
+
print(f"[Groq] β
Response OK for {role}")
|
| 61 |
+
return response.choices[0].message.content.strip()
|
| 62 |
+
except Exception as e:
|
| 63 |
+
print(f"β [Groq Error for {role}]: {e}")
|
| 64 |
+
return f"β Error: {e}"
|
| 65 |
+
|
| 66 |
+
# ---------------------------
|
| 67 |
+
# ποΈ Debate Logic
|
| 68 |
+
# ---------------------------
|
| 69 |
+
def host_debate(topic: str) -> str:
|
| 70 |
+
print(f"\nπ§ Topic Received: {topic}")
|
| 71 |
+
if not topic.strip():
|
| 72 |
+
return "β Please enter a valid debate topic."
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
print("β Generating Pro argument...")
|
| 76 |
+
pro = query_llama("pro", topic)
|
| 77 |
+
print("β
Pro:\n", pro)
|
| 78 |
+
|
| 79 |
+
print("β Generating Con argument...")
|
| 80 |
+
con = query_llama("con", topic)
|
| 81 |
+
print("β
Con:\n", con)
|
| 82 |
+
|
| 83 |
+
print("β Moderating...")
|
| 84 |
+
verdict = query_llama("moderator", topic, pro=pro, con=con)
|
| 85 |
+
print("β
Verdict:\n", verdict)
|
| 86 |
+
|
| 87 |
+
# π£οΈ Speak results (optional)
|
| 88 |
+
speak_text(f"Debate Topic: {topic}")
|
| 89 |
+
speak_text(f"Pro says: {pro}")
|
| 90 |
+
speak_text(f"Con says: {con}")
|
| 91 |
+
speak_text(f"Moderator verdict: {verdict}")
|
| 92 |
+
|
| 93 |
+
return f"""
|
| 94 |
+
## ποΈ Topic: **{topic}**
|
| 95 |
+
|
| 96 |
+
---
|
| 97 |
+
|
| 98 |
+
### β
Pro Side
|
| 99 |
+
{pro}
|
| 100 |
+
|
| 101 |
+
---
|
| 102 |
+
|
| 103 |
+
### β Con Side
|
| 104 |
+
{con}
|
| 105 |
+
|
| 106 |
+
---
|
| 107 |
+
|
| 108 |
+
### βοΈ Moderator's Verdict
|
| 109 |
+
{verdict}
|
| 110 |
+
""".strip()
|
| 111 |
+
|
| 112 |
+
except Exception as e:
|
| 113 |
+
print(f"β Debate error: {e}")
|
| 114 |
+
return f"β Unexpected error: {e}"
|
| 115 |
+
|
| 116 |
+
# ---------------------------
|
| 117 |
+
# π Gradio Web Interface
|
| 118 |
+
# ---------------------------
|
| 119 |
+
demo = gr.Interface(
|
| 120 |
+
fn=host_debate,
|
| 121 |
+
inputs=gr.Textbox(label="Enter a Debate Topic", placeholder="e.g. Should AI be regulated?", lines=2),
|
| 122 |
+
outputs="markdown",
|
| 123 |
+
title="π€ AI Debate Moderator",
|
| 124 |
+
description="Enter any controversial topic. Two LLaMA 3 agents will debate (Pro vs Con), and a moderator will score them based on logic and clarity.",
|
| 125 |
+
allow_flagging="never"
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
if __name__ == "__main__":
|
| 129 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
groq
|
| 2 |
+
gradio
|
| 3 |
+
python-dotenv
|
| 4 |
+
gTTS
|
| 5 |
+
pyglet
|