Nagesh Muralidhar commited on
Commit
c99885f
·
1 Parent(s): 70c3b32

midterm-submission

Browse files
Files changed (1) hide show
  1. podcraft/src/pages/PodcastForm.tsx +40 -8
podcraft/src/pages/PodcastForm.tsx CHANGED
@@ -2,7 +2,9 @@ import React, { useState, useEffect, useRef } from 'react';
2
  import { useParams } from 'react-router-dom';
3
  import '../App.css';
4
 
5
- const API_URL = 'http://localhost:8000';
 
 
6
 
7
  interface Message {
8
  id: number;
@@ -15,6 +17,8 @@ interface Podcast {
15
  title: string;
16
  description: string;
17
  audio_file: string;
 
 
18
  }
19
 
20
  interface PodcastContext {
@@ -30,6 +34,7 @@ const PodcastForm: React.FC = () => {
30
  const [messages, setMessages] = useState<Message[]>([]);
31
  const [inputMessage, setInputMessage] = useState('');
32
  const [isLoading, setIsLoading] = useState(false);
 
33
  const messagesEndRef = useRef<HTMLDivElement>(null);
34
 
35
  useEffect(() => {
@@ -48,12 +53,30 @@ const PodcastForm: React.FC = () => {
48
  }
49
  const files = await response.json();
50
 
51
- const podcastList = files.map((file: any, index: number) => ({
52
- id: index + 1,
53
- title: file.filename.split('-')[0].replace(/_/g, ' '),
54
- description: "An AI-generated debate podcast exploring different perspectives",
55
- audio_file: `${API_URL}${file.path}`
56
- }));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  const selectedPodcast = podcastList.find(p => p.id === parseInt(id));
59
  if (selectedPodcast) {
@@ -68,6 +91,7 @@ const PodcastForm: React.FC = () => {
68
  }
69
  } catch (err) {
70
  console.error('Error fetching podcast:', err);
 
71
  }
72
  };
73
 
@@ -89,7 +113,7 @@ const PodcastForm: React.FC = () => {
89
  setIsLoading(true);
90
 
91
  try {
92
- const response = await fetch(`${API_URL}/podcast-chat/${id}`, {
93
  method: 'POST',
94
  headers: {
95
  'Content-Type': 'application/json',
@@ -128,9 +152,17 @@ const PodcastForm: React.FC = () => {
128
  <div className="podcast-form-container">
129
  <div className="chat-column">
130
  <div className="podcast-chat-container">
 
 
 
 
131
  {podcast && (
132
  <div className="podcast-player-header">
133
  <h2 className="podcast-title">{podcast.title}</h2>
 
 
 
 
134
  {podcastContext && (
135
  <p className="podcast-topic">Topic: {podcastContext.topic}</p>
136
  )}
 
2
  import { useParams } from 'react-router-dom';
3
  import '../App.css';
4
 
5
+ // Use relative URLs in production, full URLs in development
6
+ const isDevelopment = window.location.hostname === 'localhost';
7
+ const API_URL = isDevelopment ? 'http://localhost:8000' : '';
8
 
9
  interface Message {
10
  id: number;
 
17
  title: string;
18
  description: string;
19
  audio_file: string;
20
+ filename?: string;
21
+ category?: string;
22
  }
23
 
24
  interface PodcastContext {
 
34
  const [messages, setMessages] = useState<Message[]>([]);
35
  const [inputMessage, setInputMessage] = useState('');
36
  const [isLoading, setIsLoading] = useState(false);
37
+ const [error, setError] = useState<string>("");
38
  const messagesEndRef = useRef<HTMLDivElement>(null);
39
 
40
  useEffect(() => {
 
53
  }
54
  const files = await response.json();
55
 
56
+ const podcastList = files.map((file: any, index: number) => {
57
+ const filename = file.filename;
58
+ const parts = filename.split('-');
59
+ let queryPart = '', descriptionPart = '', categoryWithExt = '';
60
+
61
+ if (parts.length >= 3) {
62
+ [queryPart, descriptionPart, categoryWithExt] = parts;
63
+ } else {
64
+ queryPart = parts[0] || '';
65
+ descriptionPart = parts[1] || queryPart;
66
+ categoryWithExt = parts[2] || 'general.mp3';
67
+ }
68
+
69
+ const category = categoryWithExt.replace('.mp3', '');
70
+
71
+ return {
72
+ id: index + 1,
73
+ title: descriptionPart.replace(/_/g, ' ').replace(/^\w/, c => c.toUpperCase()),
74
+ description: `A debate exploring ${queryPart.replace(/_/g, ' ')}`,
75
+ audio_file: `${API_URL}${file.path}`,
76
+ filename: filename,
77
+ category: category.replace(/_/g, ' ')
78
+ };
79
+ });
80
 
81
  const selectedPodcast = podcastList.find(p => p.id === parseInt(id));
82
  if (selectedPodcast) {
 
91
  }
92
  } catch (err) {
93
  console.error('Error fetching podcast:', err);
94
+ setError(err instanceof Error ? err.message : 'Failed to fetch podcast');
95
  }
96
  };
97
 
 
113
  setIsLoading(true);
114
 
115
  try {
116
+ const response = await fetch(`${API_URL}/api/podcast-chat/${id}`, {
117
  method: 'POST',
118
  headers: {
119
  'Content-Type': 'application/json',
 
152
  <div className="podcast-form-container">
153
  <div className="chat-column">
154
  <div className="podcast-chat-container">
155
+ {error && (
156
+ <div className="error-message">Error: {error}</div>
157
+ )}
158
+
159
  {podcast && (
160
  <div className="podcast-player-header">
161
  <h2 className="podcast-title">{podcast.title}</h2>
162
+ {podcast.category && (
163
+ <div className="category-pill">{podcast.category}</div>
164
+ )}
165
+ <p className="description">{podcast.description}</p>
166
  {podcastContext && (
167
  <p className="podcast-topic">Topic: {podcastContext.topic}</p>
168
  )}