Spaces:
Sleeping
Sleeping
File size: 7,361 Bytes
d7a8925 fbb963b d7a8925 fbb963b d7a8925 d7391ba d7a8925 70c3b32 d7a8925 fbb963b d7a8925 |
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 79 80 81 82 83 84 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
import React, { useState, useEffect, useRef } from 'react';
import '../App.css';
// Add Vite env type definition
interface ImportMetaEnv {
PROD: boolean;
DEV: boolean;
}
interface Message {
id: number;
text: string;
agent: string;
audio_file?: string;
title?: string;
description?: string;
category?: string;
}
interface DebateEntry {
speaker: string;
content: string;
}
interface ChatResponse {
debate_history: DebateEntry[];
supervisor_notes: string[];
final_podcast?: {
content: string;
audio_file: string;
title: string;
description: string;
};
}
// Use relative URLs in production, full URLs in development
const isDevelopment = window.location.hostname === 'localhost';
const API_URL = isDevelopment ? 'http://localhost:8000' : '';
const Home: React.FC = () => {
const [messages, setMessages] = useState<Message[]>([
{ id: 1, text: "Welcome! I'll help you create your own podcast content by exploring topics through an AI debate. Enter any topic of choice.", agent: "system" },
]);
const [inputMessage, setInputMessage] = useState('');
const [isLoading, setIsLoading] = useState(false);
const messagesEndRef = useRef<HTMLDivElement>(null);
const audioRef = useRef<HTMLAudioElement>(null);
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
}, [messages]);
const sendMessageToServer = async (message: string): Promise<ChatResponse> => {
try {
const response = await fetch(`${API_URL}/api/chat`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
content: message,
agent_type: "believer",
context: {
podcast_id: null,
agent_chunks: [],
current_agent: "believer"
}
})
});
if (!response.ok) {
const errorData = await response.text();
console.error('Server error:', response.status, errorData);
throw new Error(`Server error (${response.status}): ${errorData}`);
}
const data: ChatResponse = await response.json();
return data;
} catch (error) {
console.error('Error sending message:', error);
throw error;
}
};
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!inputMessage.trim()) return;
const userMessage: Message = {
id: messages.length + 1,
text: inputMessage,
agent: "user"
};
setMessages(prev => [...prev, userMessage]);
setInputMessage('');
setIsLoading(true);
try {
const response = await sendMessageToServer(inputMessage);
if (response.debate_history) {
response.debate_history.forEach((entry) => {
if (entry.speaker && entry.content) {
const message: Message = {
id: messages.length + 1,
text: entry.content,
agent: entry.speaker
};
setMessages(prev => [...prev, message]);
}
});
}
if (response.supervisor_notes) {
response.supervisor_notes.forEach((note) => {
const supervisorMessage: Message = {
id: messages.length + 1,
text: note,
agent: "supervisor"
};
setMessages(prev => [...prev, supervisorMessage]);
});
}
if (response.final_podcast && response.final_podcast.audio_file) {
const filename = response.final_podcast.audio_file;
const [queryPart, descriptionPart, categoryWithExt] = filename.split('-');
const category = categoryWithExt.replace('.mp3', '');
const podcastMessage: Message = {
id: messages.length + 1,
text: response.final_podcast.content || "Podcast generated successfully!",
agent: "system",
audio_file: `/audio-files/${filename}`,
title: descriptionPart.replace(/_/g, ' ').replace(/^\w/, c => c.toUpperCase()),
description: `A debate exploring ${queryPart.replace(/_/g, ' ')}`,
category: category.replace(/_/g, ' ')
};
setMessages(prev => [...prev, podcastMessage]);
}
} catch (error) {
setMessages(prev => [...prev, {
id: prev.length + 1,
text: `Error: ${error.message}`,
agent: "system"
}]);
} finally {
setIsLoading(false);
}
};
return (
<div className="chat-container">
<div className="chat-messages">
{messages.map((message) => (
<div key={message.id} className={`message ${message.agent}-message`}>
<div className="message-content">
<div className="agent-icon">
{message.agent === "user" ? "π€" :
message.agent === "system" ? "π€" :
message.agent === "believer" ? "π‘" :
message.agent === "skeptic" ? "π€" :
message.agent === "supervisor" ? "π" :
message.agent === "extractor" ? "π" : "π¬"}
</div>
<div className="message-text-content">
<div className="agent-name">{message.agent}</div>
<div className="message-text">{message.text}</div>
{message.audio_file && (
<div className="podcast-card">
<div className="podcast-content">
<h2 className="podcast-title">{message.title || "Generated Podcast"}</h2>
{message.category && (
<div className="category-pill">{message.category}</div>
)}
<p className="description">{message.description || "An AI-generated debate podcast exploring different perspectives"}</p>
<div className="audio-player">
<audio controls src={message.audio_file} ref={audioRef}>
Your browser does not support the audio element.
</audio>
</div>
</div>
</div>
)}
</div>
</div>
</div>
))}
{isLoading && (
<div className="message system-message">
<div className="message-content">
<div className="loading-dots">Debating</div>
<div className="loading-dots">This might take a few moments since 2 agents are fighting over getting the best insights for you</div>
</div>
</div>
)}
<div ref={messagesEndRef} />
</div>
<form onSubmit={handleSubmit} className="chat-input-form">
<input
type="text"
value={inputMessage}
onChange={(e) => setInputMessage(e.target.value)}
placeholder="Type your message..."
className="chat-input"
disabled={isLoading}
/>
<button type="submit" className="chat-send-button" disabled={isLoading}>
Send
</button>
</form>
</div>
);
};
export default Home; |