Update app.py
Browse files
app.py
CHANGED
|
@@ -1,536 +1,227 @@
|
|
| 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 |
-
@keyframes ai-orbit { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
|
| 37 |
-
@keyframes ai-text-fade-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
body { font-family: var(--app-font); direction: rtl; background-color: var(--app-bg); color: var(--text-primary); font-size: 16px; line-height: 1.8; margin: 0; padding: 2.5rem 0; min-height: 100vh; display: flex; justify-content: center; align-items: flex-start; overflow-x: hidden; background-image: radial-gradient(var(--text-tertiary) 0.5px, transparent 0.5px); background-size: 20px 20px; background-position: -10px -10px; }
|
| 48 |
-
.app-container { max-width: 820px; width: 92%; margin: 0 auto; }
|
| 49 |
-
.app-header { padding: 0.5rem 0 2.5rem 0; text-align: center; margin-bottom: 1.5rem; animation: fadeIn 0.8s 0.2s ease-out backwards; }
|
| 50 |
-
.app-header h1 { font-size: 2.5em; font-weight: 900; margin: 0 0 0.8rem 0; background: linear-gradient(90deg, var(--accent-primary), var(--accent-secondary)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; letter-spacing: -1px; }
|
| 51 |
-
.app-header p { font-size: 1.05em; color: var(--text-secondary); margin-top: 0; opacity: 0.9; font-weight: 400; line-height: 1.7; }
|
| 52 |
-
.main-content { position:relative; padding: 2.5rem; background-color: var(--panel-bg); border-radius: var(--radius-card); box-shadow: var(--shadow-xl); border: 1px solid var(--panel-border); animation: fadeIn 0.8s 0.4s ease-out backwards; }
|
| 53 |
-
.form-group { margin-bottom: 2.5rem; }
|
| 54 |
-
.form-group-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1.2rem; }
|
| 55 |
-
label { display: block; font-weight: 700; color: var(--text-primary); font-size: 1.2em; }
|
| 56 |
-
#project-speakers-container { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 1.5rem; text-align: center; }
|
| 57 |
-
.speaker-display-card { position: relative; background-color: var(--input-bg); border-radius: var(--radius-card); padding: 1.5rem 1rem; border: 2px solid var(--input-border); transition: var(--transition-smooth); }
|
| 58 |
-
.speaker-display-card:hover:not(#add-speaker-card) { border-color: var(--accent-primary); transform: translateY(-5px); box-shadow: var(--shadow-md); }
|
| 59 |
-
.speaker-display-card img { width: 80px; height: 80px; border-radius: 50%; object-fit: cover; border: 4px solid var(--panel-bg); box-shadow: var(--shadow-md); margin-bottom: 1rem; }
|
| 60 |
-
.speaker-display-card h3 { margin: 0 0 0.25rem 0; font-size: 1.1em; font-weight: 800; }
|
| 61 |
-
.remove-speaker-btn { position: absolute; top: 12px; left: 12px; background: rgba(255, 255, 255, 0.7); backdrop-filter: blur(4px); border: 1px solid var(--panel-border); color: var(--text-secondary); width: 28px; height: 28px; border-radius: 50%; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 1.2rem; line-height: 1; transition: all 0.2s; }
|
| 62 |
-
.remove-speaker-btn:hover { color: var(--danger-color); transform: scale(1.1); background: #fff; }
|
| 63 |
-
#add-speaker-card { display: flex; flex-direction: column; align-items: center; justify-content: center; font-weight: 700; color: var(--accent-primary); border: 2px dashed var(--input-border); cursor: pointer; background-color: transparent; }
|
| 64 |
-
#add-speaker-card:hover { border-color: var(--accent-primary); background: var(--accent-primary-glow); }
|
| 65 |
-
#add-speaker-card .plus-icon { font-size: 2.5rem; line-height: 1; margin-bottom: 0.5rem; }
|
| 66 |
-
#podcast-script-container { border: 1px solid var(--input-border); border-radius: var(--radius-card); padding: 1rem; background-color: var(--input-bg); box-shadow: inset 0 2px 4px rgba(0,0,0,0.02); }
|
| 67 |
-
.script-turn { display: flex; gap: 1rem; margin-bottom: 1.5rem; background-color: var(--panel-bg); padding: 1rem; border-radius: var(--radius-input); border: 1px solid var(--panel-border); box-shadow: var(--shadow-md); }
|
| 68 |
-
.script-turn:last-child { margin-bottom: 0; }
|
| 69 |
-
.turn-speaker-selector { flex-shrink: 0; width: 180px; position: relative; }
|
| 70 |
-
.turn-content { flex-grow: 1; }
|
| 71 |
-
.turn-content textarea { width: 100%; min-height: 100px; resize: vertical; padding: 0.8rem 1rem; border-radius: var(--radius-input); border: 1px solid var(--input-border); background-color: #fff; font-family: var(--app-font); font-size: 1rem; box-sizing: border-box; }
|
| 72 |
-
.turn-content textarea:focus { outline: none; border-color: var(--accent-primary); box-shadow: 0 0 0 3px var(--accent-primary-glow); }
|
| 73 |
-
.remove-turn-btn { flex-shrink: 0; background: none; border: none; color: var(--text-tertiary); font-size: 1.5rem; cursor: pointer; align-self: center; transition: all 0.2s; }
|
| 74 |
-
.remove-turn-btn:hover { color: var(--danger-color); transform: scale(1.1); }
|
| 75 |
-
#add-turn-btn { display: flex; align-items: center; justify-content: center; gap: 8px; width: 100%; padding: 1rem; font-size: 1em; font-weight: 700; background: transparent; color: var(--text-primary); border: 2px dashed var(--input-border); border-radius: var(--radius-btn); cursor: pointer; margin-top: 1.5rem; transition: var(--transition-smooth); }
|
| 76 |
-
#add-turn-btn:hover { background: var(--panel-bg); border-color: var(--accent-primary); color: var(--accent-primary); }
|
| 77 |
-
.slider-container { display: flex; align-items: center; gap: 1.5rem; }
|
| 78 |
-
input[type="range"] { flex-grow: 1; -webkit-appearance: none; appearance: none; width: 100%; height: 6px; background: var(--input-border); border-radius: 3px; outline: none; cursor: pointer; }
|
| 79 |
-
input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; appearance: none; width: 24px; height: 24px; background: #fff; border-radius: 50%; cursor: pointer; border: 4px solid var(--accent-primary); box-shadow: var(--shadow-md); margin-top: -9px; }
|
| 80 |
-
.temperature-value { font-weight: 700; background-color: var(--input-bg); padding: 0.5rem 1rem; border-radius: 8px; border: 1px solid var(--input-border); min-width: 45px; text-align: center; color: var(--accent-primary); }
|
| 81 |
-
.generate-btn { display: flex; align-items: center; justify-content: center; gap: 10px; width: 100%; padding: 1.1rem 1.5rem; font-size: 1.25em; font-weight: 800; background: linear-gradient(95deg, var(--accent-secondary) 0%, var(--accent-primary) 100%); color: #fff; border: none; border-radius: var(--radius-btn); cursor: pointer; transition: all 0.3s ease; box-shadow: 0 6px 12px -3px var(--accent-primary-glow), 0 6px 12px -3px var(--accent-secondary-glow); }
|
| 82 |
-
.generate-btn:hover:not(:disabled) { transform: translateY(-5px) scale(1.02); box-shadow: 0 8px 20px -4px var(--accent-primary-glow), 0 8px 20px -4px var(--accent-secondary-glow); }
|
| 83 |
-
.generate-btn:disabled { background: var(--text-tertiary); cursor: not-allowed; box-shadow: none; }
|
| 84 |
-
.generate-btn .spinner { width: 20px; height: 20px; border: 3px solid rgba(255, 255, 255, 0.4); border-top-color: #fff; border-radius: 50%; animation: spin 0.8s linear infinite; display: none;}
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
.
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
.modal-overlay.visible { display: flex; opacity: 1; }
|
| 147 |
-
.modal-dialog { background: var(--panel-bg); padding: 2.5rem; border-radius: var(--radius-card); width: 90%; max-width: 750px; max-height: 85vh; box-shadow: var(--shadow-lg); border: 1px solid var(--panel-border); animation: modalZoomIn 0.4s cubic-bezier(0.4, 0, 0.2, 1) forwards; position: relative; display: flex; flex-direction: column; }
|
| 148 |
-
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem; padding-bottom: 1.5rem; border-bottom: 1px solid var(--panel-border); }
|
| 149 |
-
.modal-header h2 { margin: 0; font-size: 1.8em; font-weight: 800; color: var(--accent-primary); }
|
| 150 |
-
.close-modal-btn { background: none; border: none; font-size: 2.5rem; cursor: pointer; color: var(--text-tertiary); transition: var(--transition-smooth); line-height: 1; }
|
| 151 |
-
.close-modal-btn:hover { color: var(--accent-primary); transform: rotate(90deg); }
|
| 152 |
-
.modal-body { overflow-y: auto; }
|
| 153 |
-
#speaker-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 1.5rem; }
|
| 154 |
-
.speaker-card { cursor: pointer; transition: var(--transition-smooth); text-align: center; padding: 0.5rem; }
|
| 155 |
-
.speaker-card:hover:not(.disabled) { transform: translateY(-8px); }
|
| 156 |
-
.speaker-card .speaker-visual { border: 3px solid transparent; border-radius: 18px; overflow: hidden; box-shadow: var(--shadow-md); transition: var(--transition-smooth); }
|
| 157 |
-
.speaker-card:hover:not(.disabled) .speaker-visual { box-shadow: var(--shadow-lg); }
|
| 158 |
-
.speaker-card img { width: 100%; height: 130px; object-fit: cover; display: block; background-color: #e0e0e0; border-radius: 14px; }
|
| 159 |
-
.speaker-card .speaker-name { padding: 0.8rem 0.4rem 0.2rem; font-weight: 600; font-size: 0.95em; color: var(--text-secondary); }
|
| 160 |
-
.speaker-card.disabled { opacity: 0.4; cursor: not-allowed; }
|
| 161 |
-
.custom-select-trigger { display: flex; align-items: center; gap: 0.75rem; padding: 0.5rem; border-radius: var(--radius-input); border: 1px solid var(--input-border); background-color: #fff; cursor: pointer; transition: var(--transition-smooth); }
|
| 162 |
-
.custom-select-trigger:hover { border-color: var(--accent-primary); }
|
| 163 |
-
.custom-select-trigger img { width: 40px; height: 40px; border-radius: 10px; object-fit: cover; }
|
| 164 |
-
.custom-select-trigger span { font-weight: 600; flex-grow: 1; }
|
| 165 |
-
.custom-select-trigger .arrow { margin-right: auto; width: 20px; height: 20px; color: var(--text-tertiary); transition: transform 0.2s; }
|
| 166 |
-
.custom-select-container.open .arrow { transform: rotate(180deg); }
|
| 167 |
-
.custom-select-options { position: absolute; top: calc(100% + 8px); left: 0; width: 100%; background: var(--panel-bg); border-radius: var(--radius-input); border: 1px solid var(--panel-border); box-shadow: var(--shadow-lg); z-index: 10; max-height: 250px; overflow-y: auto; opacity: 0; transform: translateY(10px); pointer-events: none; transition: all 0.25s ease-out; }
|
| 168 |
-
.custom-select-container.open .custom-select-options { opacity: 1; transform: translateY(0); pointer-events: auto; }
|
| 169 |
-
.custom-select-option { display: flex; align-items: center; gap: 0.75rem; padding: 0.75rem; cursor: pointer; }
|
| 170 |
-
.custom-select-option:hover { background-color: var(--input-bg); }
|
| 171 |
-
.custom-select-option img { width: 40px; height: 40px; border-radius: 10px; object-fit: cover; }
|
| 172 |
-
.custom-select-option span { font-weight: 500; }
|
| 173 |
-
.no-speaker-option { padding: 1rem; text-align: center; color: var(--text-secondary); }
|
| 174 |
-
.ai-script-btn { background: transparent; border: 1px solid var(--accent-primary); color: var(--accent-primary); padding: 0.4rem 0.9rem; font-size: 0.9em; border-radius: var(--radius-btn); font-weight: 700; cursor: pointer; transition: var(--transition-smooth); display: flex; align-items: center; gap: 8px; }
|
| 175 |
-
.ai-script-btn:hover { background: var(--accent-primary-glow); box-shadow: var(--shadow-md); }
|
| 176 |
-
#ai-modal .modal-dialog { max-width: 480px; padding: 32px 36px; min-height: 480px; display: flex; align-items: center; }
|
| 177 |
-
#ai-modal .modal-content-wrapper, #ai-modal .modal-loading-wrapper { width: 100%; transition: opacity 0.4s ease-in-out, transform 0.4s ease-in-out; }
|
| 178 |
-
#ai-modal .modal-content-wrapper:not(.active), #ai-modal .modal-loading-wrapper:not(.active) { opacity: 0; transform: scale(0.95); pointer-events: none; position: absolute; right: 36px; left: 36px; }
|
| 179 |
-
#ai-modal .modal-loading-wrapper { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 40px; }
|
| 180 |
-
#ai-modal .loader-animation { width: 120px; height: 120px; position: relative; display: flex; align-items: center; justify-content: center; }
|
| 181 |
-
#ai-modal .core-pulse { width: 25px; height: 25px; background-color: var(--accent-primary); border-radius: 50%; animation: ai-core-pulse 2s infinite cubic-bezier(0.45, 0.05, 0.55, 0.95); }
|
| 182 |
-
#ai-modal .ai-orbit { position: absolute; border-radius: 50%; border: 2px dashed var(--panel-border); animation: ai-orbit 10s linear infinite; }
|
| 183 |
-
#ai-modal .ai-orbit:nth-child(2) { width: 70px; height: 70px; animation-duration: 8s; animation-direction: reverse; }
|
| 184 |
-
#ai-modal .ai-orbit:nth-child(3) { width: 110px; height: 110px; animation-duration: 12s; }
|
| 185 |
-
#ai-modal .ai-orbit::before { content: ''; position: absolute; width: 10px; height: 10px; border-radius: 50%; background-color: var(--text-primary); top: 5px; left: 5px; }
|
| 186 |
-
#ai-modal .ai-orbit:nth-child(2)::before { background-color: var(--accent-secondary); top: auto; bottom: 8px; left: 8px; }
|
| 187 |
-
#ai-modal .ai-orbit:nth-child(3)::before { background-color: var(--accent-primary); top: 10px; right: 10px; left: auto; }
|
| 188 |
-
#loading-text-ai { font-size: 18px; font-weight: 600; background-image: linear-gradient(100deg, var(--accent-primary), var(--accent-secondary)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; text-align: center; min-height: 27px; animation: ai-text-fade-in 0.5s ease; }
|
| 189 |
-
#ai-modal .modal-header { padding-bottom: 0; border-bottom: none; margin-bottom: 0; }
|
| 190 |
-
#ai-modal .modal-header h2 { font-size: 28px; font-weight: 800; }
|
| 191 |
-
#ai-modal .modal-body p { color: var(--text-secondary); line-height: 1.8; margin: 24px 0; font-size: 1rem; }
|
| 192 |
-
#ai-modal textarea { width: 100%; min-height: 150px; padding: 16px; border: 2px solid var(--input-border); border-radius: var(--radius-input); font-family: var(--app-font); font-size: 1rem; resize: vertical; box-sizing: border-box; background-color: #fff; transition: border-color 0.3s ease, box-shadow 0.3s ease; }
|
| 193 |
-
#ai-modal textarea:focus { outline: none; border-color: var(--accent-primary); box-shadow: 0 0 0 4px var(--accent-primary-glow); }
|
| 194 |
-
#ai-modal .modal-footer { margin-top: 28px; padding-top: 0; border-top: none; }
|
| 195 |
-
#generate-script-ai-btn { width: 100%; padding: 16px 24px; font-family: var(--app-font); font-size: 18px; font-weight: 700; background-image: linear-gradient(100deg, var(--accent-primary), var(--accent-secondary)); background-size: 200% 100%; box-shadow: 0 8px 20px -5px var(--accent-primary-glow); transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1), box-shadow 0.3s cubic-bezier(0.16, 1, 0.3, 1), background-position 0.4s ease; }
|
| 196 |
-
#generate-script-ai-btn:hover { background-position: right center; }
|
| 197 |
-
#ai-status-message { margin: 1rem 0 0 0; text-align: center; display: none; }
|
| 198 |
-
#ai-status-message.error { color: var(--danger-color); font-weight: 700; }
|
| 199 |
-
|
| 200 |
-
/* --- استایل مودال تایید (Confirmation Modal) --- */
|
| 201 |
-
#confirm-modal .modal-dialog { max-width: 450px; text-align: center; }
|
| 202 |
-
#confirm-modal .modal-body { padding-top: 1rem; }
|
| 203 |
-
#confirm-modal-message { font-size: 1.1em; color: var(--text-secondary); margin-bottom: 2rem; }
|
| 204 |
-
.confirm-modal-actions { display: flex; gap: 1rem; justify-content: center; }
|
| 205 |
-
.confirm-btn { padding: 0.8rem 1.5rem; font-family: var(--app-font); font-size: 1em; font-weight: 700; border-radius: var(--radius-btn); border: none; cursor: pointer; transition: var(--transition-smooth); }
|
| 206 |
-
#confirm-btn-yes { background-color: var(--danger-color); color: white; box-shadow: 0 4px 12px -2px var(--danger-glow); }
|
| 207 |
-
#confirm-btn-yes:hover { background-color: var(--danger-color-hover); transform: translateY(-3px); }
|
| 208 |
-
#confirm-btn-no { background-color: var(--input-bg); color: var(--text-primary); border: 1px solid var(--input-border); }
|
| 209 |
-
#confirm-btn-no:hover { background-color: var(--panel-border); }
|
| 210 |
-
|
| 211 |
-
@media (max-width: 768px) { .script-turn { flex-direction: column; } .remove-turn-btn { align-self: flex-end; margin-top: -1.5rem; } .turn-speaker-selector { width: 100%; } }
|
| 212 |
-
</style>
|
| 213 |
-
</head>
|
| 214 |
-
<body>
|
| 215 |
-
<div class="app-container">
|
| 216 |
-
<header class="app-header">
|
| 217 |
-
<h1>استودیوی ساخت پادکست</h1>
|
| 218 |
-
<p>گویندگان خود را به پروژه اضافه کنید، سناریو را ب��ویسید و پادکست خود را تحویل بگیرید.</p>
|
| 219 |
-
</header>
|
| 220 |
-
<main class="main-content">
|
| 221 |
-
<form id="podcast-form" onsubmit="return false;">
|
| 222 |
-
<!-- ... سایر بخشهای فرم ... -->
|
| 223 |
-
<div class="form-group">
|
| 224 |
-
<label>🎤 تیم گویندگان</label>
|
| 225 |
-
<div id="project-speakers-container">
|
| 226 |
-
<div id="add-speaker-card" class="speaker-display-card">
|
| 227 |
-
<div class="plus-icon">+</div>
|
| 228 |
-
<h3>افزودن گوینده</h3>
|
| 229 |
-
</div>
|
| 230 |
-
</div>
|
| 231 |
-
</div>
|
| 232 |
-
<div class="form-group">
|
| 233 |
-
<div class="form-group-header">
|
| 234 |
-
<label>📜 سناریوی گفتگو</label>
|
| 235 |
-
<button type="button" id="open-ai-modal-btn" class="ai-script-btn">
|
| 236 |
-
✨ساخت پروژه با هوش مصنوعی
|
| 237 |
-
</button>
|
| 238 |
-
</div>
|
| 239 |
-
<div id="podcast-script-container"></div>
|
| 240 |
-
<button type="button" id="add-turn-btn">+ افزودن نوبت گفتگو</button>
|
| 241 |
-
</div>
|
| 242 |
-
<div class="form-group">
|
| 243 |
-
<label for="temperature-slider-podcast">🌡️ خلاقیت صدا</label>
|
| 244 |
-
<div class="slider-container">
|
| 245 |
-
<input type="range" id="temperature-slider-podcast" min="0.1" max="1.5" step="0.05" value="0.9">
|
| 246 |
-
<span id="temperature-value-podcast" class="temperature-value">0.9</span>
|
| 247 |
-
</div>
|
| 248 |
-
</div>
|
| 249 |
-
|
| 250 |
-
<button type="submit" id="generate-btn-podcast" class="generate-btn">
|
| 251 |
-
<span class="btn-text">🎙️ ساخت پادکست</span>
|
| 252 |
-
<div class="spinner"></div>
|
| 253 |
-
</button>
|
| 254 |
-
<!-- پیام اعتبارسنجی اینجا نمایش داده میشود -->
|
| 255 |
-
<p id="validation-message" style="display: none;"></p>
|
| 256 |
-
</form>
|
| 257 |
-
<div id="output-section-podcast" class="output-section">
|
| 258 |
-
<div id="status-message-podcast" class="status-message">پادکست نهایی در اینجا ظاهر خواهد شد.</div>
|
| 259 |
-
<button type="button" id="clear-history-btn" style="display: none;">
|
| 260 |
-
<svg viewBox="0 0 24 24"><path d="M6 19c0 1.1.9 2 2 2h8c1.1 0 2-.9 2-2V7H6v12zM19 4h-3.5l-1-1h-5l-1 1H5v2h14V4z"></path></svg>
|
| 261 |
-
حذف سابقه گفتگو قبلی
|
| 262 |
-
</button>
|
| 263 |
-
<div id="loading-animation-wrapper-podcast" class="loading-animation-wrapper">
|
| 264 |
-
<div class="orbital-loader">
|
| 265 |
-
<div class="orbit"><div class="satellite"></div></div>
|
| 266 |
-
<div class="orbit"><div class="satellite"></div></div>
|
| 267 |
-
<div class="orbit"><div class="satellite"></div></div>
|
| 268 |
-
</div>
|
| 269 |
-
<p class="loading-text" id="loading-text-podcast">در حال پردازش هوشمند...</p>
|
| 270 |
-
</div>
|
| 271 |
-
<div id="audio-player-content-podcast" class="audio-player-content">
|
| 272 |
-
<div class="audio-waveform-container"> <span class="audio-time audio-current-time">0:00</span> <div class="audio-waveform"> <canvas class="audio-waveform-canvas"></canvas> <div class="audio-waveform-dashed-line"></div> </div> <span class="audio-time audio-total-time">0:00</span> </div> <div class="audio-controls-group"> <button type="button" class="audio-skip-btn backward" title="پرش به عقب"> <svg viewBox="0 0 24 24"><path d="M11 16V8l-4 4 4 4zm4-12v16l7-8-7-8z"></path></svg> </button> <button type="button" class="audio-play-pause-btn-large"> <svg viewBox="0 0 24 24" class="play-icon"><path d="M8 5v14l11-7z"></path></svg> <svg viewBox="0 0 24 24" class="pause-icon" style="display:none;"><path d="M6 19h4V5H6v14zm8-14v14h4V5h-4z"></path></svg> </button> <button type="button" class="audio-skip-btn forward" title="پرش به جلو"> <svg viewBox="0 0 24 24"><path d="M13 16V8l4 4-4 4zM9 4v16L2 12l7-8z"></path></svg> </button> </div> <div class="audio-utility-controls"> <button type="button" class="audio-volume-btn" title="قطع/وصل صدا"> <svg viewBox="0 0 24 24" class="volume-high-icon"><path d="M3 9v6h4l5 5V4L7 9H3zm13.5 3c0-1.77-1.02-3.29-2.5-4.03v8.05c1.48-.73 2.5-2.25 2.5-4.02zM14 3.23v2.06c2.89.86 5 3.54 5 6.71s-2.11 5.85-5 6.71v2.06c4.01-.91 7-4.49 7-8.77s-2.99-7.86-7-8.77z"></path></svg> <svg viewBox="0 0 24 24" class="volume-mute-icon" style="display:none;"><path d="M7 9v6h4l5 5V4L11 9H7zM16.5 12c0-1.77-1.02-3.29-2.5-4.03v2.21l2.45 2.45c.03-.2.05-.41.05-.63zM19 12c0 .94-.23 1.82-.68 2.6L19 14.88c.45-.88.7-1.88.7-2.88 0-4.01-2.99-7.14-7-8.05v2.06c2.89.86 5 3.54 5 6.71zM4.55 4L2 6.55 9.45 14H7v6h4l5 5V14.55l4.05 4.05L22 18 12 8 4.55 4z"></path></svg> </button> <button type="button" class="audio-speed-btn" title="سرعت پخش">1x</button> </div>
|
| 273 |
-
</div>
|
| 274 |
-
</div>
|
| 275 |
-
</main>
|
| 276 |
-
<audio id="hidden-audio-player" style="display: none;"></audio>
|
| 277 |
-
|
| 278 |
-
<!-- مودال انتخاب گوینده -->
|
| 279 |
-
<div id="speaker-modal" class="modal-overlay">
|
| 280 |
-
<div class="modal-dialog">
|
| 281 |
-
<div class="modal-header">
|
| 282 |
-
<h2 id="modal-title">انتخاب گوینده برای افزودن به پروژه</h2>
|
| 283 |
-
<button type="button" class="close-modal-btn">×</button>
|
| 284 |
-
</div>
|
| 285 |
-
<div class="modal-body"><div id="speaker-grid"></div></div>
|
| 286 |
-
</div>
|
| 287 |
-
</div>
|
| 288 |
-
|
| 289 |
-
<!-- مودال هوش مصنوعی -->
|
| 290 |
-
<div id="ai-modal" class="modal-overlay">
|
| 291 |
-
<div class="modal-dialog">
|
| 292 |
-
<div id="ai-content-wrapper" class="modal-content-wrapper active">
|
| 293 |
-
<div class="modal-header">
|
| 294 |
-
<h2>ساخت پروژه با هوش مصنوعی</h2>
|
| 295 |
-
<button type="button" class="close-modal-btn">×</button>
|
| 296 |
-
</div>
|
| 297 |
-
<div class="modal-body">
|
| 298 |
-
<p>یک موضوع (مثلاً: "تاریخچه پیدایش قهوه") یا یک مقاله کامل را وارد کنید. هوش مصنوعی بهترین تیم گویندگان را انتخاب کرده و سناریوی گفتگو را تولید خواهد کرد.</p>
|
| 299 |
-
<textarea id="ai-prompt-textarea" placeholder="موضوع یا متن مقاله خود را اینجا وارد کنید..."></textarea>
|
| 300 |
-
<p id="ai-status-message"></p>
|
| 301 |
-
</div>
|
| 302 |
-
<div class="modal-footer">
|
| 303 |
-
<button type="button" id="generate-script-ai-btn" class="generate-btn">
|
| 304 |
-
🚀 پروژه را بساز
|
| 305 |
-
</button>
|
| 306 |
-
</div>
|
| 307 |
-
</div>
|
| 308 |
-
<div id="ai-loading-wrapper" class="modal-loading-wrapper">
|
| 309 |
-
<div class="loader-animation">
|
| 310 |
-
<div class="core-pulse"></div>
|
| 311 |
-
<div class="ai-orbit"></div>
|
| 312 |
-
<div class="ai-orbit"></div>
|
| 313 |
-
</div>
|
| 314 |
-
<p id="loading-text-ai">در حال آمادهسازی...</p>
|
| 315 |
-
</div>
|
| 316 |
-
</div>
|
| 317 |
-
</div>
|
| 318 |
-
|
| 319 |
-
<!-- مودال جدید برای تایید حذف -->
|
| 320 |
-
<div id="confirm-modal" class="modal-overlay">
|
| 321 |
-
<div class="modal-dialog">
|
| 322 |
-
<div class="modal-header">
|
| 323 |
-
<h2>تایید عملیات</h2>
|
| 324 |
-
</div>
|
| 325 |
-
<div class="modal-body">
|
| 326 |
-
<p id="confirm-modal-message">آیا از انجام این کار مطمئن هستید؟</p>
|
| 327 |
-
<div class="confirm-modal-actions">
|
| 328 |
-
<button id="confirm-btn-no" class="confirm-btn">لغو</button>
|
| 329 |
-
<button id="confirm-btn-yes" class="confirm-btn">تایید حذف</button>
|
| 330 |
-
</div>
|
| 331 |
-
</div>
|
| 332 |
-
</div>
|
| 333 |
-
</div>
|
| 334 |
-
</div>
|
| 335 |
-
|
| 336 |
-
<script>
|
| 337 |
-
document.addEventListener('DOMContentLoaded', () => {
|
| 338 |
-
// ... تمام کدهای JS قبلی شما بدون تغییر اینجا قرار میگیرد ...
|
| 339 |
-
const TTS_API_ENDPOINT = 'https://alphattspro1.onrender.com/api/generate';
|
| 340 |
-
const AI_CREATE_API_ENDPOINT = '/api/create-full-podcast';
|
| 341 |
-
const AI_STATUS_API_ENDPOINT = '/api/podcast-status/';
|
| 342 |
-
const BATCH_SIZE = 2;
|
| 343 |
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
let activeSpeakers = [], masterAudioBlobs = [], currentlyPlayingTurnPlayer = null, speakerToReplaceId = null;
|
| 347 |
-
let audioPeaks = [], currentPlaybackSpeedIndex = 0;
|
| 348 |
-
const playbackSpeeds = [1.0, 1.25, 1.5, 0.75];
|
| 349 |
-
const mainAudioPlayer = document.getElementById('hidden-audio-player');
|
| 350 |
-
const form = document.getElementById('podcast-form');
|
| 351 |
-
const scriptContainer = document.getElementById('podcast-script-container');
|
| 352 |
-
const addTurnBtn = document.getElementById('add-turn-btn');
|
| 353 |
-
const tempSlider = document.getElementById('temperature-slider-podcast');
|
| 354 |
-
const tempValueSpan = document.getElementById('temperature-value-podcast');
|
| 355 |
-
const generateBtn = document.getElementById('generate-btn-podcast');
|
| 356 |
-
const outputSection = document.getElementById('output-section-podcast');
|
| 357 |
-
const statusMessage = document.getElementById('status-message-podcast');
|
| 358 |
-
const clearHistoryBtn = document.getElementById('clear-history-btn');
|
| 359 |
-
const loadingAnimationWrapper = document.getElementById('loading-animation-wrapper-podcast');
|
| 360 |
-
const loadingText = document.getElementById('loading-text-podcast');
|
| 361 |
-
const playerContent = document.getElementById('audio-player-content-podcast');
|
| 362 |
-
const speakerModal = document.getElementById('speaker-modal');
|
| 363 |
-
const modalTitleElement = document.getElementById('modal-title');
|
| 364 |
-
const addSpeakerCard = document.getElementById('add-speaker-card');
|
| 365 |
-
const projectSpeakersContainer = document.getElementById('project-speakers-container');
|
| 366 |
-
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
|
| 367 |
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
const openAiModalBtn = document.getElementById('open-ai-modal-btn');
|
| 371 |
-
const generateAiBtn = document.getElementById('generate-script-ai-btn');
|
| 372 |
-
const aiPromptTextarea = document.getElementById('ai-prompt-textarea');
|
| 373 |
-
const aiStatusMessage = document.getElementById('ai-status-message');
|
| 374 |
-
const aiContentWrapper = document.getElementById('ai-content-wrapper');
|
| 375 |
-
const aiLoadingWrapper = document.getElementById('ai-loading-wrapper');
|
| 376 |
-
const aiLoadingText = document.getElementById('loading-text-ai');
|
| 377 |
-
const aiStatusMessages = ["در حال تحلیل موضوع...", "انتخاب هوشمند گویندگان...", "نوشتن پیشنویس سناریو...", "بازبینی و نهاییسازی متن...", "آمادهسازی پروژه..."];
|
| 378 |
-
let pollingInterval = null, statusInterval = null;
|
| 379 |
-
|
| 380 |
-
// --- متغیرهای جدید برای مودال تایید و پیام خطا ---
|
| 381 |
-
const confirmModal = document.getElementById('confirm-modal');
|
| 382 |
-
const confirmMessageEl = document.getElementById('confirm-modal-message');
|
| 383 |
-
const confirmBtnYes = document.getElementById('confirm-btn-yes');
|
| 384 |
-
const confirmBtnNo = document.getElementById('confirm-btn-no');
|
| 385 |
-
const validationMessage = document.getElementById('validation-message');
|
| 386 |
-
let validationTimeout;
|
| 387 |
-
|
| 388 |
-
const showAiModal = () => aiModal.classList.add('visible');
|
| 389 |
-
const hideAiModal = () => {
|
| 390 |
-
aiModal.classList.remove('visible');
|
| 391 |
-
resetAiModalView();
|
| 392 |
-
};
|
| 393 |
-
openAiModalBtn.addEventListener('click', showAiModal);
|
| 394 |
-
aiModal.querySelector('.close-modal-btn').addEventListener('click', hideAiModal);
|
| 395 |
-
aiModal.addEventListener('click', e => { if (e.target === aiModal) hideAiModal(); });
|
| 396 |
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
confirmMessageEl.textContent = message;
|
| 432 |
-
confirmModal.classList.add('visible');
|
| 433 |
-
|
| 434 |
-
const handleConfirm = () => {
|
| 435 |
-
onConfirm();
|
| 436 |
-
hideConfirmModal();
|
| 437 |
-
};
|
| 438 |
-
|
| 439 |
-
const hideConfirmModal = () => {
|
| 440 |
-
confirmModal.classList.remove('visible');
|
| 441 |
-
confirmBtnYes.removeEventListener('click', handleConfirm);
|
| 442 |
-
confirmBtnNo.removeEventListener('click', hideConfirmModal);
|
| 443 |
-
};
|
| 444 |
-
|
| 445 |
-
confirmBtnYes.addEventListener('click', handleConfirm, { once: true });
|
| 446 |
-
confirmBtnNo.addEventListener('click', hideConfirmModal, { once: true });
|
| 447 |
-
}
|
| 448 |
-
|
| 449 |
-
function showValidationError(message) {
|
| 450 |
-
clearTimeout(validationTimeout);
|
| 451 |
-
validationMessage.textContent = message;
|
| 452 |
-
validationMessage.style.display = 'block';
|
| 453 |
-
|
| 454 |
-
generateBtn.classList.add('shake-animation-btn');
|
| 455 |
-
validationMessage.classList.add('validation-message-active');
|
| 456 |
-
|
| 457 |
-
setTimeout(() => {
|
| 458 |
-
generateBtn.classList.remove('shake-animation-btn');
|
| 459 |
-
}, 600); // زمان انیمیشن لرزش
|
| 460 |
-
|
| 461 |
-
validationTimeout = setTimeout(() => {
|
| 462 |
-
validationMessage.classList.remove('validation-message-active');
|
| 463 |
-
validationMessage.style.display = 'none';
|
| 464 |
-
}, 4000); // زمان نمایش پیام
|
| 465 |
-
}
|
| 466 |
-
|
| 467 |
-
clearHistoryBtn.addEventListener('click', () => {
|
| 468 |
-
showConfirmModal(
|
| 469 |
-
'آیا از حذف سابقه و بازنشانی پروژه مطمئن هستید؟',
|
| 470 |
-
resetToDefaultState
|
| 471 |
-
);
|
| 472 |
-
});
|
| 473 |
-
|
| 474 |
-
document.addEventListener('click', e => { const isDropdownTrigger = e.target.closest('.custom-select-trigger'); const isDropdownOption = e.target.closest('.custom-select-option'); document.querySelectorAll('.custom-select-container.open').forEach(container => { if (!container.contains(e.target)) { container.classList.remove('open'); } }); if (isDropdownTrigger) { const container = isDropdownTrigger.closest('.custom-select-container'); container.classList.toggle('open'); } else if (isDropdownOption) { const container = isDropdownOption.closest('.custom-select-container'); const speakerId = isDropdownOption.dataset.id; const speaker = activeSpeakers.find(s => s.id === speakerId); if(speaker){ const trigger = container.querySelector('.custom-select-trigger'); trigger.innerHTML = `<img src="${speaker.imgUrl}" alt="${speaker.name}"><span>${speaker.name}</span><svg class="arrow" viewBox="0 0 24 24"><path fill="currentColor" d="M7,10L12,15L17,10H7Z"></path></svg>`; container.dataset.selectedId = speaker.id; container.classList.remove('open'); saveState(); } } });
|
| 475 |
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
index: index,
|
| 482 |
-
speakerId: t.querySelector('.custom-select-container').dataset.selectedId,
|
| 483 |
-
text: t.querySelector('textarea').value.trim()
|
| 484 |
-
}))
|
| 485 |
-
.filter(d => d.text.length > 0 && d.speakerId);
|
| 486 |
-
|
| 487 |
-
if (activeSpeakers.length === 0) {
|
| 488 |
-
showValidationError('لطفا ابتدا حداقل یک گوینده به پروژه اضافه کنید.');
|
| 489 |
-
return;
|
| 490 |
-
}
|
| 491 |
-
if (scriptData.length === 0) {
|
| 492 |
-
showValidationError('لطفاً سناریو را کامل کنید و برای هر بخش یک گوینده انتخاب نمایید.');
|
| 493 |
-
return;
|
| 494 |
-
}
|
| 495 |
-
|
| 496 |
-
showUIState('loading', 'در حال آمادهسازی...');
|
| 497 |
-
masterAudioBlobs = new Array(scriptData.length).fill(null);
|
| 498 |
-
try {
|
| 499 |
-
for (let i = 0; i < scriptData.length; i += BATCH_SIZE) {
|
| 500 |
-
const batch = scriptData.slice(i, i + BATCH_SIZE);
|
| 501 |
-
const totalBatches = Math.ceil(scriptData.length / BATCH_SIZE);
|
| 502 |
-
showUIState('loading', `در حال پردازش دسته ${ (i / BATCH_SIZE) + 1 } از ${ totalBatches }...`);
|
| 503 |
-
const batchPromises = batch.map(segment => {
|
| 504 |
-
const payload = { text: segment.text, speaker: segment.speakerId, temperature: parseFloat(tempSlider.value) };
|
| 505 |
-
return fetch(TTS_API_ENDPOINT, { method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify(payload) })
|
| 506 |
-
.then(res => res.ok ? res.blob() : res.text().then(text => { throw new Error(`خطا در قطعه ${segment.index + 1}: ${text}`) }))
|
| 507 |
-
.then(blob => ({ index: segment.index, blob }));
|
| 508 |
-
});
|
| 509 |
-
const results = await Promise.all(batchPromises);
|
| 510 |
-
results.forEach(result => { masterAudioBlobs[result.index] = result.blob; });
|
| 511 |
-
}
|
| 512 |
-
showUIState('loading', 'همه قطعهها ساخته شد. در حال میکس نهایی صدا...');
|
| 513 |
-
const mergedBuffer = await mergeAudioBlobs(masterAudioBlobs);
|
| 514 |
-
if (!mergedBuffer) throw new Error("میکس صدا با شکست مواجه شد (هیچ فایل صوتی معتبری دریافت نشد).");
|
| 515 |
-
const finalWavBlob = bufferToWav(mergedBuffer);
|
| 516 |
-
if(mainAudioPlayer.src) URL.revokeObjectURL(mainAudioPlayer.src);
|
| 517 |
-
mainAudioPlayer.src = URL.createObjectURL(finalWavBlob);
|
| 518 |
-
mainAudioPlayer.load();
|
| 519 |
-
mainAudioPlayer.onloadedmetadata = () => {
|
| 520 |
-
processAudioForWaveform(mergedBuffer);
|
| 521 |
-
showUIState('result');
|
| 522 |
-
updateMainPlayerUI();
|
| 523 |
-
};
|
| 524 |
-
scriptData.forEach(item => { setupTurnPlayer(item.element, item.index); });
|
| 525 |
-
} catch (error) {
|
| 526 |
-
console.error(error);
|
| 527 |
-
showUIState('error', `عملیات با شکست مواجه شد: ${error.message}`);
|
| 528 |
-
}
|
| 529 |
-
});
|
| 530 |
-
|
| 531 |
-
setupMainPlayerListeners();
|
| 532 |
-
loadState();
|
| 533 |
-
});
|
| 534 |
-
</script>
|
| 535 |
-
</body>
|
| 536 |
-
</html>
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import json
|
| 4 |
+
import time
|
| 5 |
+
from flask import Flask, request, jsonify, render_template
|
| 6 |
+
from flask_cors import CORS
|
| 7 |
+
import uuid
|
| 8 |
+
import threading
|
| 9 |
+
from datetime import datetime, timedelta
|
| 10 |
+
import re
|
| 11 |
+
import logging
|
| 12 |
+
import random # 1. کتابخانه random برای به هم ریختن لیست اضافه شد
|
| 13 |
+
|
| 14 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
|
| 15 |
+
|
| 16 |
+
app = Flask(__name__)
|
| 17 |
+
CORS(app)
|
| 18 |
+
|
| 19 |
+
tasks = {}
|
| 20 |
+
tasks_lock = threading.Lock()
|
| 21 |
+
|
| 22 |
+
GEMINI_API_KEYS = []
|
| 23 |
+
key_index = 0
|
| 24 |
+
key_lock = threading.Lock()
|
| 25 |
+
|
| 26 |
+
request_counter = 0
|
| 27 |
+
request_counter_lock = threading.Lock()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def cleanup_old_tasks():
|
| 31 |
+
with tasks_lock:
|
| 32 |
+
tasks_to_delete = []
|
| 33 |
+
for task_id, task_data in list(tasks.items()):
|
| 34 |
+
if datetime.utcnow() - task_data.get('timestamp', datetime.utcnow()) > timedelta(minutes=15):
|
| 35 |
+
tasks_to_delete.append(task_id)
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
for task_id in tasks_to_delete:
|
| 38 |
+
if task_id in tasks:
|
| 39 |
+
del tasks[task_id]
|
| 40 |
+
|
| 41 |
+
def generate_podcast_in_background(task_id, system_prompt, safety_settings, api_key):
|
| 42 |
+
try:
|
| 43 |
+
genai.configure(api_key=api_key)
|
| 44 |
+
model_instance = genai.GenerativeModel('gemini-2.5-pro')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
cleanup_old_tasks()
|
| 47 |
+
max_retries = 3
|
| 48 |
+
for attempt in range(max_retries):
|
| 49 |
+
try:
|
| 50 |
+
response = model_instance.generate_content(
|
| 51 |
+
system_prompt,
|
| 52 |
+
safety_settings=safety_settings
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
if not response.parts:
|
| 56 |
+
raise ValueError("Response was blocked by safety filters or was empty.")
|
| 57 |
+
|
| 58 |
+
raw_text = response.text
|
| 59 |
+
|
| 60 |
+
json_string = None
|
| 61 |
+
match = re.search(r"```json\s*(\{.*?\})\s*```", raw_text, re.DOTALL)
|
| 62 |
+
if match:
|
| 63 |
+
json_string = match.group(1)
|
| 64 |
+
else:
|
| 65 |
+
start_index = raw_text.find('{')
|
| 66 |
+
end_index = raw_text.rfind('}')
|
| 67 |
+
if start_index != -1 and end_index != -1 and end_index > start_index:
|
| 68 |
+
json_string = raw_text[start_index : end_index + 1]
|
| 69 |
+
|
| 70 |
+
if not json_string:
|
| 71 |
+
raise ValueError("Could not find a valid JSON object in the AI response.")
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
response_json = json.loads(json_string)
|
| 75 |
+
except json.JSONDecodeError as json_err:
|
| 76 |
+
raise ValueError(f"AI returned malformed JSON. Details: {json_err}")
|
| 77 |
+
|
| 78 |
+
if not isinstance(response_json, dict) or "selected_speakers" not in response_json or "script" not in response_json:
|
| 79 |
+
raise ValueError("Generated JSON has an incorrect structure (missing keys).")
|
| 80 |
+
|
| 81 |
+
for turn in response_json.get("script", []):
|
| 82 |
+
if "dialogue" in turn and isinstance(turn["dialogue"], str):
|
| 83 |
+
cleaned_dialogue = re.sub(r'\[.*?\]|\(.*?\)', '', turn["dialogue"])
|
| 84 |
+
turn["dialogue"] = cleaned_dialogue.strip()
|
| 85 |
+
|
| 86 |
+
with tasks_lock:
|
| 87 |
+
tasks[task_id].update({'status': 'completed', 'data': response_json})
|
| 88 |
+
return
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
logging.warning(f"Task {task_id}: Attempt {attempt + 1} failed: {e}")
|
| 92 |
+
if attempt >= max_retries - 1:
|
| 93 |
+
raise e
|
| 94 |
+
time.sleep(2)
|
| 95 |
+
|
| 96 |
+
except Exception as e:
|
| 97 |
+
error_message = str(e)
|
| 98 |
+
with tasks_lock:
|
| 99 |
+
tasks[task_id].update({'status': 'failed', 'error': f"AI failed to generate a valid response: {error_message}"})
|
| 100 |
+
logging.error(f"Task {task_id} failed permanently: {error_message}")
|
| 101 |
+
|
| 102 |
+
try:
|
| 103 |
+
keys_str = os.environ.get("ALL_GEMINI_API_KEYS")
|
| 104 |
+
if not keys_str:
|
| 105 |
+
raise ValueError("سکرت ALL_GEMINI_API_KEYS پیدا نشد.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
+
GEMINI_API_KEYS = [key.strip() for key in keys_str.split(',') if key.strip()]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
if not GEMINI_API_KEYS:
|
| 110 |
+
raise ValueError("هیچ کلید معتبری در متغیر ALL_GEMINI_API_KEYS یافت نشد.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
logging.info("===========================================")
|
| 113 |
+
logging.info(f"✅ تعداد {len(GEMINI_API_KEYS)} کلید جیمینای با موفقیت شناسایی شد.")
|
| 114 |
+
logging.info("===========================================")
|
| 115 |
+
|
| 116 |
+
safety_settings = [
|
| 117 |
+
{"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
|
| 118 |
+
{"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
|
| 119 |
+
{"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
|
| 120 |
+
{"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
|
| 121 |
+
]
|
| 122 |
+
|
| 123 |
+
except Exception as e:
|
| 124 |
+
logging.error(f"❌ خطا در هنگام پیکربندی اولیه: {e}")
|
| 125 |
+
GEMINI_API_KEYS = []
|
| 126 |
+
|
| 127 |
+
@app.route('/')
|
| 128 |
+
def index():
|
| 129 |
+
return render_template('index.html')
|
| 130 |
+
|
| 131 |
+
@app.route('/api/create-full-podcast', methods=['POST'])
|
| 132 |
+
def create_full_podcast():
|
| 133 |
+
if not GEMINI_API_KEYS:
|
| 134 |
+
return jsonify({"error": "مدل Gemini به دلیل نبود کلید API معتبر، راهاندازی نشده است. لاگهای سرور را بررسی کنید."}), 500
|
| 135 |
+
|
| 136 |
+
try:
|
| 137 |
+
global request_counter
|
| 138 |
+
global key_index
|
| 139 |
+
|
| 140 |
+
with request_counter_lock:
|
| 141 |
+
request_counter += 1
|
| 142 |
+
current_request_number = request_counter
|
| 143 |
+
|
| 144 |
+
with key_lock:
|
| 145 |
+
current_key_index = key_index
|
| 146 |
+
selected_api_key = GEMINI_API_KEYS[current_key_index]
|
| 147 |
+
key_index = (key_index + 1) % len(GEMINI_API_KEYS)
|
| 148 |
+
|
| 149 |
+
logging.info(f"🚀 درخواست #{current_request_number}: استفاده از کلید شماره {current_key_index + 1}")
|
| 150 |
+
|
| 151 |
+
data = request.get_json()
|
| 152 |
+
user_prompt = data.get('prompt')
|
| 153 |
+
available_speakers = data.get('available_speakers')
|
| 154 |
+
|
| 155 |
+
if not user_prompt or not available_speakers:
|
| 156 |
+
return jsonify({"error": "Prompt and speakers list are required."}), 400
|
| 157 |
+
|
| 158 |
+
# 2. به هم ریختن ترتیب لیست گویندگان برای جلوگیری از بایاس
|
| 159 |
+
random.shuffle(available_speakers)
|
| 160 |
+
|
| 161 |
+
speaker_info_text = "\n".join(
|
| 162 |
+
f"- ID: '{s['id']}', Name: {s['name']}, Gender: {s['gender']}" for s in available_speakers
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
# 3. بهبود و تقویت پرامپت برای انتخاب هوشمندانه
|
| 166 |
+
system_prompt = f"""
|
| 167 |
+
You are an expert Podcast Producer. Your task is to act as a Casting Director and a Script Writer.
|
| 168 |
+
|
| 169 |
+
Step 1: Casting Director.
|
| 170 |
+
Analyze the user's topic: "{user_prompt}"
|
| 171 |
+
Review the list of available voice talents below. Your selection MUST be logical and suitable for the topic. For example, for a technical topic, you might choose more male voices, and for a social topic, a mix of male and female voices.
|
| 172 |
+
Select a DIVERSE and suitable team of 2 to 4 speakers. AVOID defaulting to the same speakers for different topics. Your choices should be varied and intelligent.
|
| 173 |
+
|
| 174 |
+
Available Voice Talents:
|
| 175 |
+
{speaker_info_text}
|
| 176 |
+
|
| 177 |
+
Step 2: Script Writer. Write a complete, engaging podcast script for the team you just selected.
|
| 178 |
+
|
| 179 |
+
**CRITICAL OUTPUT INSTRUCTIONS:**
|
| 180 |
+
- Your entire response MUST BE a single, valid JSON object and nothing else.
|
| 181 |
+
- The JSON must have "selected_speakers" and "script" keys.
|
| 182 |
+
|
| 183 |
+
**CRITICAL SCRIPT CONTENT RULES:**
|
| 184 |
+
- The "dialogue" values MUST ONLY contain the words to be spoken.
|
| 185 |
+
- DO NOT include ANY descriptions of sounds, music, actions, or emotions in brackets or parentheses (e.g., "[موسیقی]", "(با خنده)").
|
| 186 |
+
- The output must be clean and ready for a Text-to-Speech (TTS) engine.
|
| 187 |
+
|
| 188 |
+
Example of the ONLY valid output format:
|
| 189 |
+
{{
|
| 190 |
+
"selected_speakers": ["Charon", "Vindemiatrix"],
|
| 191 |
+
"script": [
|
| 192 |
+
{{"speaker_id": "Charon", "dialogue": "سلام به همه شنوندگان عزیز پادکست ما."}},
|
| 193 |
+
{{"speaker_id": "Vindemiatrix", "dialogue": "سلام شهاب جان. خوشحالم که امروز هم با یک موضوع جذاب دیگر در خدمت شما هستیم."}}
|
| 194 |
+
]
|
| 195 |
+
}}
|
| 196 |
+
"""
|
| 197 |
+
|
| 198 |
+
task_id = str(uuid.uuid4())
|
| 199 |
+
with tasks_lock:
|
| 200 |
+
tasks[task_id] = {'status': 'pending', 'timestamp': datetime.utcnow()}
|
| 201 |
+
|
| 202 |
+
thread = threading.Thread(
|
| 203 |
+
target=generate_podcast_in_background,
|
| 204 |
+
args=(task_id, system_prompt, safety_settings, selected_api_key)
|
| 205 |
+
)
|
| 206 |
+
thread.daemon = True
|
| 207 |
+
thread.start()
|
| 208 |
+
|
| 209 |
+
return jsonify({"task_id": task_id}), 202
|
| 210 |
+
|
| 211 |
+
except Exception as e:
|
| 212 |
+
logging.error(f"CRITICAL ERROR in API endpoint: {e}")
|
| 213 |
+
return jsonify({"error": "An unexpected internal server error occurred."}), 500
|
| 214 |
+
|
| 215 |
+
@app.route('/api/podcast-status/<task_id>', methods=['GET'])
|
| 216 |
+
def get_podcast_status(task_id):
|
| 217 |
+
with tasks_lock:
|
| 218 |
+
task = tasks.get(task_id)
|
| 219 |
|
| 220 |
+
if not task:
|
| 221 |
+
return jsonify({'status': 'not_found', 'error': 'Task not found or expired.'}), 404
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
+
return jsonify(task)
|
| 224 |
+
|
| 225 |
+
if __name__ == '__main__':
|
| 226 |
+
port = int(os.environ.get('PORT', 7860))
|
| 227 |
+
app.run(host='0.0.0.0', port=port)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|