Luongsosad commited on
Commit
1e6d966
·
1 Parent(s): 07daa2a
Files changed (4) hide show
  1. .env +4 -0
  2. README.md +4 -4
  3. app.py +150 -0
  4. requirements.txt +2 -0
.env ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ PINECONE_API_KEY="pcsk_4zsak4_FQdKjqt8na92LSaHghkLB3QJmdCCmm7L86A7jt3g3UvvzSChQ41YYXcCgFZteLs"
2
+ GROQ_API_KEY="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"
3
+ DB_INDEX = "tpwits-website-data"
4
+ LANGCHAIN_PROJECT="TPWits AI Agent"
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Script
3
- emoji: 🏆
4
- colorFrom: yellow
5
- colorTo: purple
6
  sdk: gradio
7
  sdk_version: 5.29.0
8
  app_file: app.py
 
1
  ---
2
+ title: Podcast Bot
3
+ emoji: 🔥
4
+ colorFrom: gray
5
+ colorTo: blue
6
  sdk: gradio
7
  sdk_version: 5.29.0
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import gradio as gr
4
+ import re
5
+
6
+ # === API SETUP ===
7
+ GROQ_API_KEY = "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"
8
+ GROQ_API_URL = "https://api.groq.com/openai/v1/chat/completions"
9
+
10
+ HEADERS = {
11
+ "Authorization": f"Bearer {GROQ_API_KEY}",
12
+ "Content-Type": "application/json"
13
+ }
14
+
15
+ # === BLOCK INAPPROPRIATE CONTENT ===
16
+ BLOCKED_KEYWORDS = [
17
+ "sex", "drugs", "violence", "suicide", "death", "kill", "murder", "gun",
18
+ "abuse", "politics", "terror", "crime", "war", "religion", "racism",
19
+ "nude", "adult", "nsfw"
20
+ ]
21
+
22
+ def is_safe_topic(topic: str) -> bool:
23
+ topic_lower = topic.lower()
24
+ return not any(bad_word in topic_lower for bad_word in BLOCKED_KEYWORDS)
25
+
26
+ # === PODCAST GENERATION ===
27
+ def generate_educational_podcast(topic: str, model: str = "llama3-70b-8192") -> str:
28
+ prompt = f"""
29
+ You are a scriptwriter for an educational podcast aimed at school students in grades 8 to 12.
30
+
31
+ Create a short and engaging podcast script on the topic: "{topic}".
32
+
33
+ The two hosts are named Ali and Talha. Ali starts the conversation.
34
+ They should have a back-and-forth conversation about the topic.
35
+ The tone should be friendly, easy to understand, and informative.
36
+
37
+ Avoid music or sound effects. Keep the total length under 800 words.
38
+ Only write their dialogue and a few narration lines if needed. Do NOT use Host 1/2 or any generic names.
39
+ """
40
+
41
+ data = {
42
+ "model": model,
43
+ "messages": [
44
+ {"role": "system", "content": "You are a creative and age-appropriate educational podcast writer."},
45
+ {"role": "user", "content": prompt.strip()}
46
+ ],
47
+ "temperature": 0.75,
48
+ "max_tokens": 1024
49
+ }
50
+
51
+ response = requests.post(GROQ_API_URL, headers=HEADERS, json=data)
52
+
53
+ if response.status_code == 200:
54
+ return response.json()["choices"][0]["message"]["content"].strip()
55
+ else:
56
+ return f"<b>Error {response.status_code}</b>: {response.text}"
57
+
58
+ # === FORMAT SCRIPT CLEANLY FOR DISPLAY ===
59
+ def format_script(script: str) -> str:
60
+ # Remove markdown bolding like **text**
61
+ script = re.sub(r'\*\*(.*?)\*\*', r'\1', script)
62
+
63
+ # Remove music and direction lines
64
+ script = re.sub(r'(?i)^.*(music|sound effect).*$','', script, flags=re.MULTILINE)
65
+
66
+ # Normalize host names to Ali and Talha
67
+ script = script.replace("Host 1:", "Ali:")
68
+ script = script.replace("Host 2:", "Talha:")
69
+ script = re.sub(r'\b[Aa]lex:', 'Ali:', script)
70
+ script = re.sub(r'\b[Mm]aya:', 'Talha:', script)
71
+
72
+ lines = script.strip().split("\n")
73
+ formatted = ""
74
+
75
+ for line in lines:
76
+ line = line.strip()
77
+ if not line:
78
+ continue
79
+ # Only keep lines that are spoken by Ali or Talha
80
+ if line.startswith("Ali:"):
81
+ content = line.replace("Ali:", "").strip()
82
+ formatted += f"<div class='host1'>🎙️ <b>Ali:</b> {content}</div>\n"
83
+ elif line.startswith("Talha:"):
84
+ content = line.replace("Talha:", "").strip()
85
+ formatted += f"<div class='host2'>🎧 <b>Talha:</b> {content}</div>\n"
86
+
87
+ return formatted
88
+
89
+ # === FINAL FUNCTION ===
90
+ def generate(topic):
91
+ if not is_safe_topic(topic):
92
+ return "<b style='color:red;'>⚠️ Restricted content. Please enter an appropriate educational topic.</b>"
93
+ raw_script = generate_educational_podcast(topic)
94
+ return format_script(raw_script)
95
+
96
+ # === DARK GLOSSY CSS ===
97
+ custom_css = """
98
+ # body {
99
+ # background-color: darkslateblue !important;
100
+ # color: #ffffff;
101
+ # }
102
+
103
+ # .gradio-container {
104
+ # background-color: darkslateblue !important;
105
+ # }
106
+
107
+ # textarea, input, .gr-button {
108
+ # background-color: darkslategray !important;
109
+ # color: #ffffff !important;
110
+ # border: 1px solid #aaa !important;
111
+ # }
112
+
113
+ .host1, .host2 {
114
+ border-radius: 16px;
115
+ padding: 14px;
116
+ margin: 16px 0;
117
+ box-shadow: 0 4px 10px rgba(255,255,255,0.2);
118
+ font-size: 16px;
119
+ line-height: 1.6;
120
+ color: #ffffff;
121
+ }
122
+
123
+ .host1 {
124
+ background: linear-gradient(135deg, #4b6cb7, #182848); /* Dark blue gradient */
125
+ }
126
+
127
+ .host2 {
128
+ background: linear-gradient(135deg, #2c3e50, #4ca1af); /* Slate gray gradient */
129
+ }
130
+
131
+ .narration {
132
+ font-style: italic;
133
+ color: #cccccc;
134
+ margin: 10px 0;
135
+ font-size: 15px;
136
+ }
137
+ """
138
+
139
+ # === GRADIO INTERFACE ===
140
+ with gr.Blocks(css=custom_css) as demo:
141
+ gr.Markdown("## 🎙️ Educational Podcast Generator for Students", elem_id="title")
142
+ gr.Markdown("Enter an educational topic, and enjoy a friendly podcast conversation between Ali and Talha.")
143
+
144
+ topic_input = gr.Textbox(label="Enter educational topic")
145
+ generate_btn = gr.Button("🎧 Generate Podcast")
146
+ output_box = gr.HTML()
147
+
148
+ generate_btn.click(fn=generate, inputs=topic_input, outputs=output_box)
149
+
150
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ requests