karthigakannaiyan commited on
Commit
18cc9d9
Β·
verified Β·
1 Parent(s): b6d020d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +129 -0
  2. 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