Skip to content

50+ open-source generative AI apps you can clone, deploy, and monetize — image generators, video tools, virtual try-ons, AI SaaS templates, and platform integrations. One-click Vercel deploy on every template.

23.3 KB
Raw Blame
"use client";

import { useState } from "react";
import { FaMicrophone, FaDownload, FaFileAlt, FaImage, FaVideo } from "react-icons/fa";
import { FiRefreshCw } from "react-icons/fi";
import axios from "axios";
import toast from "react-hot-toast";
import config from "@/lib/config";

function CustomSelect({ value, onChange, options, placeholder = "Select option", className = "" }) {
  const [open, setOpen] = useState(false);
  const selectedOption = options.find(opt => opt.value === value) || { label: placeholder, value };
  
  return (
    <div className={`relative ${className}`}>
      <button
        type="button"
        onClick={() => setOpen(!open)}
        className="w-full bg-bg-page border border-divider/60 rounded py-2.5 px-3 text-xs outline-none focus:border-primary/60 transition-all font-semibold text-primary-text flex items-center justify-between cursor-pointer select-none active:scale-[0.99] min-h-[38px]"
      >
        <span>{selectedOption.label}</span>
        <span className={`text-[8px] transition-transform duration-200 ${open ? "rotate-180" : ""}`}>▼</span>
      </button>

      {open && (
        <>
          <div className="fixed inset-0 z-40" onClick={() => setOpen(false)} />
          <div className="absolute left-0 right-0 mt-1 bg-bg-card border border-divider rounded shadow-2xl z-50 py-1 max-h-48 overflow-y-auto scrollbar-subtle animate-scale-up">
            {options.map((opt) => (
              <button
                key={opt.value}
                type="button"
                onClick={() => {
                  onChange(opt.value);
                  setOpen(false);
                }}
                className={`w-full text-left px-3 py-2 text-xs transition-colors hover:bg-primary/10 hover:text-primary cursor-pointer ${
                  value === opt.value ? "text-primary font-bold bg-primary/5" : "text-primary-text"
                }`}
              >
                {opt.label}
              </button>
            ))}
          </div>
        </>
      )}
    </div>
  );
}

export default function AudioTemplate({ appInstance, userCredits, activeCreation, onCreationCompleted, generating: propGenerating, setGenerating: propSetGenerating }) {
  const parsedConfig = appInstance.config ? JSON.parse(appInstance.config) : {};
  const userParams = parsedConfig.userParams || [];

  const [audioUrl, setAudioUrl] = useState(null);
  const [uploading, setUploading] = useState(false);
  const [localGenerating, setLocalGenerating] = useState(false);
  const generating = propGenerating !== undefined ? propGenerating : localGenerating;
  const setGenerating = propSetGenerating !== undefined ? propSetGenerating : setLocalGenerating;

  // Dynamic Parameter State
  const [customValues, setCustomValues] = useState(() => {
    const initial = {};
    userParams.forEach(p => {
      initial[p.key] = p.defaultValue !== undefined ? p.defaultValue : "";
    });
    return initial;
  });

  // Calculate dynamic credit cost
  const getDynamicCost = () => {
    let baseCost = parsedConfig.creditCost !== undefined ? Number(parsedConfig.creditCost) : config.ai.generationCost;
    let totalCost = baseCost;

    userParams.forEach(p => {
      const val = customValues[p.key] !== undefined ? customValues[p.key] : p.defaultValue;

      if (p.type === "enum") {
        if (p.costModifiers && p.costModifiers[val] !== undefined) {
          totalCost += Number(p.costModifiers[val]) || 0;
        } else if (Array.isArray(p.costModifiers) && p.options) {
          const optIndex = p.options.indexOf(val);
          if (optIndex !== -1 && p.costModifiers[optIndex] !== undefined) {
            totalCost += Number(p.costModifiers[optIndex]) || 0;
          }
        }
      } else if (p.type === "boolean") {
        const isTrue = val === true || val === "true" || val === 1 || val === "1";
        if (isTrue && p.costIfTrue !== undefined) {
          totalCost += Number(p.costIfTrue) || 0;
        }
      } else if (p.type === "number" || p.type === "slider") {
        if (p.costPerUnit !== undefined) {
          const numVal = Number(val) || 0;
          totalCost += numVal * (Number(p.costPerUnit) || 0);
        }
      }
    });

    return Math.max(0, totalCost);
  };

  const handleAudioUpload = async (e) => {
    const file = e.target.files?.[0];
    if (!file) return;

    setUploading(true);
    const formData = new FormData();
    formData.append("file", file);

    try {
      const { data } = await axios.post("/api/upload", formData);
      setAudioUrl(data.url);
      toast.success("Audio file loaded!");
    } catch (err) {
      toast.error("Failed to upload audio.");
    } finally {
      setUploading(false);
    }
  };

  const handleDynamicFileUpload = async (e, key, maxInputs = 1, fileTypeLabel = "File") => {
    const file = e.target.files?.[0];
    if (!file) return;

    setUploading(true);
    const formData = new FormData();
    formData.append("file", file);

    try {
      const { data } = await axios.post("/api/upload", formData);
      setCustomValues(prev => {
        const currentList = Array.isArray(prev[key]) ? prev[key] : [];
        return {
          ...prev,
          [key]: [...currentList, data.url].slice(0, maxInputs)
        };
      });
      toast.success(`${fileTypeLabel} uploaded successfully!`);
    } catch (err) {
      toast.error(`Failed to upload ${fileTypeLabel.toLowerCase()}.`);
    } finally {
      setUploading(false);
    }
  };

  const removeDynamicFile = (key, idx) => {
    setCustomValues(prev => {
      const currentList = Array.isArray(prev[key]) ? prev[key] : [];
      return {
        ...prev,
        [key]: currentList.filter((_, i) => i !== idx)
      };
    });
  };

  const handleTranscribe = async (e) => {
    e.preventDefault();

    let finalPrompt = parsedConfig.systemPrompt || "Transcribe the following audio accurately, retaining all verbal statements.";
    if (userParams.length > 0) {
      const promptParam = userParams.find(p => p.key === "prompt");
      if (promptParam) {
        finalPrompt = customValues["prompt"] || "";
      }
    }

    let inputAudioVal = audioUrl;

    // Gather custom params
    const customParams = {};
    if (userParams.length > 0) {
      userParams.forEach(p => {
        if (p.key !== "prompt") {
          const isUploadType = ["image_list", "video_list", "audio_list"].includes(p.type);
          if (isUploadType) {
            const fileList = Array.isArray(customValues[p.key]) ? customValues[p.key] : [];
            const isListKey = p.key.endsWith("_list") || (p.maxInputs && p.maxInputs > 1);

            if (isListKey) {
              customParams[p.key] = fileList;
            } else {
              customParams[p.key] = fileList[0] || "";
            }

            if (p.type === "audio_list" && !inputAudioVal) {
              inputAudioVal = fileList[0] || null;
            }
          } else {
            customParams[p.key] = customValues[p.key];
          }
        }
      });
    }

    if (!inputAudioVal) {
      toast.error("Please upload an audio file first.");
      return;
    }

    setGenerating(true);
    const toastId = toast.loading("Processing audio transcription...");

    try {
      const { data } = await axios.post("/api/generation", {
        prompt: finalPrompt,
        inputImage: inputAudioVal, // Pass audio link in reference parameter (inputImage)
        appId: appInstance.id,
        modelEndpoint: "predictions", // Whisper predictions
        ...customParams
      });

      if (data.status === "failed") {
        toast.error("Transcription failed. Credits refunded.", { id: toastId });
      } else if (data.status === "completed") {
        toast.success("Transcription complete!", { id: toastId });
      } else {
        toast.success("Transcription started! Polling status...", { id: toastId });
      }
      onCreationCompleted(data);
    } catch (err) {
      toast.error(err.response?.data?.error || "Transcription failed.", { id: toastId });
    } finally {
      setGenerating(false);
    }
  };

  const handleDownloadTxt = (text) => {
    const element = document.createElement("a");
    const file = new Blob([text], { type: "text/plain" });
    element.href = URL.createObjectURL(file);
    element.download = `transcript_${appInstance.id}_${Date.now()}.txt`;
    document.body.appendChild(element);
    element.click();
    document.body.removeChild(element);
  };

  return (
    <div className="flex flex-col lg:flex-row gap-6 w-full max-w-7xl items-stretch">
      {/* File Upload Box */}
      <div className="w-full lg:w-[400px] shrink-0 border border-divider/40 bg-bg-card/30 p-6 rounded-lg flex flex-col gap-6">
        <div>
          <h2 className="text-sm font-extrabold uppercase tracking-wider text-primary">Upload Audio</h2>
          <p className="text-[11px] text-secondary-text">Select a speech recording or podcast file.</p>
        </div>

        <form onSubmit={handleTranscribe} className="space-y-6">
          {userParams.length > 0 ? (
            userParams.map((param) => {
              if (["image_list", "video_list", "audio_list"].includes(param.type)) {
                const urls = Array.isArray(customValues[param.key]) ? customValues[param.key] : (customValues[param.key] ? [customValues[param.key]] : []);
                const maxInps = param.maxInputs || 1;

                let fileAccept = "image/*";
                let icon = <FaImage className="text-xl" />;
                let labelText = "Image";
                if (param.type === "video_list") {
                  fileAccept = "video/*";
                  icon = <FaVideo className="text-xl" />;
                  labelText = "Video";
                } else if (param.type === "audio_list") {
                  fileAccept = "audio/*";
                  icon = <FaMicrophone className="text-xl" />;
                  labelText = "Audio";
                }

                return (
                  <div key={param.key} className="space-y-3">
                    <div className="flex justify-between items-center">
                      <label className="text-xs font-bold text-secondary-text uppercase tracking-wider">{param.label}</label>
                      <span className="text-[10px] text-secondary-text font-bold bg-bg-page px-2 py-0.5 rounded border border-divider">
                        {urls.length}/{maxInps} {maxInps > 1 ? "Files" : "File"}
                      </span>
                    </div>

                    {urls.length > 0 && (
                      <div className="grid grid-cols-3 gap-2">
                        {urls.map((url, idx) => (
                          <div key={idx} className="relative aspect-square border border-divider rounded bg-bg-page/80 overflow-hidden group">
                            {param.type === "image_list" ? (
                              <img src={url} alt="Uploaded source" className="w-full h-full object-cover" />
                            ) : param.type === "video_list" ? (
                              <div className="w-full h-full flex items-center justify-center bg-black">
                                <FaVideo size={16} className="text-primary" />
                              </div>
                            ) : (
                              <div className="w-full h-full flex items-center justify-center bg-bg-card">
                                <FaMicrophone size={16} className="text-primary" />
                              </div>
                            )}
                            <button
                              type="button"
                              onClick={() => removeDynamicFile(param.key, idx)}
                              className="absolute top-1 right-1 bg-red-500/80 hover:bg-red-500 text-white rounded-full w-4 h-4 flex items-center justify-center text-[9px] font-bold cursor-pointer transition-colors"
                            >
                              ✕
                            </button>
                          </div>
                        ))}
                      </div>
                    )}

                    {urls.length < maxInps && (
                      <div className="relative border-2 border-dashed border-divider hover:border-primary/50 transition-colors rounded-lg h-24 flex flex-col items-center justify-center bg-bg-page/40 p-2">
                        <label className="cursor-pointer flex flex-col items-center gap-1.5 text-xs font-semibold text-secondary-text">
                          {icon}
                          <span className="text-[10px]">{uploading ? "Uploading..." : `Upload ${labelText}`}</span>
                          <input
                            type="file"
                            onChange={(e) => handleDynamicFileUpload(e, param.key, maxInps, labelText)}
                            className="hidden"
                            accept={fileAccept}
                            disabled={uploading}
                          />
                        </label>
                      </div>
                    )}
                  </div>
                );
              }

              if (param.type === "boolean") {
                return (
                  <div key={param.key} className="flex items-center justify-between py-2 border-b border-divider/20">
                    <div className="space-y-0.5">
                      <span className="text-xs font-bold text-primary-text block">{param.label}</span>
                      <span className="text-[10px] text-secondary-text">Toggle control</span>
                    </div>
                    <label className="relative inline-flex items-center cursor-pointer">
                      <input
                        type="checkbox"
                        checked={!!customValues[param.key]}
                        onChange={(e) => setCustomValues(prev => ({ ...prev, [param.key]: e.target.checked }))}
                        className="sr-only peer"
                      />
                      <div className="w-9 h-5 bg-bg-page peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-secondary-text peer-checked:after:bg-white after:border-none after:rounded-full after:h-4 after:w-4 after:transition-all peer-checked:bg-primary border border-divider"></div>
                    </label>
                  </div>
                );
              }

              if (param.type === "enum") {
                const options = param.options || [];
                return (
                  <div key={param.key} className="space-y-2">
                    <label className="text-xs font-bold text-secondary-text uppercase tracking-wider block">{param.label}</label>
                    <CustomSelect
                      value={customValues[param.key]}
                      onChange={(val) => setCustomValues(prev => ({ ...prev, [param.key]: val }))}
                      options={options.map(opt => ({ label: opt, value: opt }))}
                    />
                  </div>
                );
              }

              if (param.type === "slider") {
                const val = customValues[param.key] !== undefined ? customValues[param.key] : param.defaultValue;
                return (
                  <div key={param.key} className="space-y-2">
                    <div className="flex justify-between items-center">
                      <label className="text-xs font-bold text-secondary-text uppercase tracking-wider">{param.label}</label>
                      <span className="text-xs font-bold text-primary">{val}</span>
                    </div>
                    <input
                      type="range"
                      min={param.min !== undefined ? param.min : 0}
                      max={param.max !== undefined ? param.max : 100}
                      step={param.step !== undefined ? param.step : 1}
                      value={val}
                      onChange={(e) => setCustomValues(prev => ({ ...prev, [param.key]: Number(e.target.value) }))}
                      className="w-full accent-primary h-1.5 bg-bg-page rounded-lg appearance-none cursor-pointer border border-divider"
                    />
                  </div>
                );
              }

              if (param.type === "textarea") {
                return (
                  <div key={param.key} className="space-y-2">
                    <label className="text-xs font-bold text-secondary-text uppercase tracking-wider block">{param.label}</label>
                    <textarea
                      value={customValues[param.key]}
                      onChange={(e) => setCustomValues(prev => ({ ...prev, [param.key]: e.target.value }))}
                      placeholder={`Enter ${param.label.toLowerCase()}...`}
                      className="w-full bg-bg-page border border-divider rounded p-3 text-xs outline-none focus:border-primary/60 transition-colors h-24 resize-none font-medium placeholder-secondary-text leading-relaxed"
                    />
                  </div>
                );
              }

              if (param.type === "number") {
                return (
                  <div key={param.key} className="space-y-2">
                    <label className="text-xs font-bold text-secondary-text uppercase tracking-wider block">{param.label}</label>
                    <input
                      type="number"
                      value={customValues[param.key]}
                      onChange={(e) => setCustomValues(prev => ({ ...prev, [param.key]: Number(e.target.value) }))}
                      className="w-full bg-bg-page border border-divider rounded py-2.5 px-3 text-xs outline-none focus:border-primary/60 transition-colors font-medium text-primary-text"
                    />
                  </div>
                );
              }

              return (
                <div key={param.key} className="space-y-2">
                  <label className="text-xs font-bold text-secondary-text uppercase tracking-wider block">{param.label}</label>
                  <input
                    type="text"
                    value={customValues[param.key]}
                    onChange={(e) => setCustomValues(prev => ({ ...prev, [param.key]: e.target.value }))}
                    placeholder={`Enter ${param.label.toLowerCase()}...`}
                    className="w-full bg-bg-page border border-divider rounded py-2.5 px-3 text-xs outline-none focus:border-primary/60 transition-colors font-medium text-primary-text"
                  />
                </div>
              );
            })
          ) : (
            <div className="space-y-2">
              <label className="text-xs font-bold text-secondary-text uppercase tracking-wider">Audio file (.mp3, .wav, .m4a)</label>
              <div className="relative border-2 border-dashed border-divider hover:border-primary/50 transition-colors rounded-lg h-36 flex flex-col items-center justify-center bg-bg-page/40 p-4">
                {audioUrl ? (
                  <div className="w-full h-full flex flex-col items-center justify-center gap-2 relative group p-2 text-center">
                    <FaMicrophone className="text-3xl text-primary animate-pulse" />
                    <span className="text-[10px] text-secondary-text truncate w-full">File Uploaded</span>
                    <button
                      type="button"
                      onClick={() => setAudioUrl(null)}
                      className="absolute inset-0 bg-black/85 opacity-0 group-hover:opacity-100 flex items-center justify-center text-xs text-white font-bold transition-opacity rounded"
                    >
                      Change File
                    </button>
                  </div>
                ) : (
                  <label className="cursor-pointer flex flex-col items-center gap-2 text-xs font-semibold text-secondary-text">
                    <FaMicrophone className="text-2xl" />
                    <span>{uploading ? "Loading File..." : "Select File"}</span>
                    <input type="file" onChange={handleAudioUpload} className="hidden" accept="audio/*" disabled={uploading} />
                  </label>
                )}
              </div>
            </div>
          )}

          <button
            type="submit"
            disabled={generating || uploading || (userParams.length === 0 && !audioUrl)}
            className="w-full bg-primary hover:bg-primary-hover disabled:bg-primary/50 text-white font-bold py-3 rounded-full text-xs shadow-lg flex items-center justify-center gap-2 transition-all cursor-pointer active:scale-[0.98]"
          >
            {generating ? (
              <>
                <FiRefreshCw className="animate-spin text-sm" />
                <span>Transcribing speech...</span>
              </>
            ) : (
              <>
                <FaFileAlt className="text-xs" />
                <span>
                  Transcribe Audio (Cost: {getDynamicCost()} credit{getDynamicCost() !== 1 ? "s" : ""})
                </span>
              </>
            )}
          </button>
        </form>
      </div>

      {/* Transcript Text Result Box */}
      <div className="flex-1 border border-divider/30 bg-bg-card/10 rounded-lg p-6 flex flex-col justify-between min-h-[400px]">
        <div className="flex-1 w-full flex flex-col gap-4">
          <div className="border-b border-divider/40 pb-2">
            <h3 className="text-xs font-bold text-secondary-text uppercase tracking-wider">Transcript Output</h3>
          </div>

          <div className="flex-1 bg-bg-page border border-divider/40 rounded p-4 text-xs leading-relaxed overflow-y-auto scrollbar-subtle min-h-[250px] max-h-[350px] overscroll-contain">
            {generating || (activeCreation && activeCreation.status === "processing") ? (
              <div className="h-full flex flex-col items-center justify-center gap-3 text-secondary-text font-bold uppercase tracking-wider">
                <div className="w-8 h-8 border-2 border-primary border-t-transparent rounded-full animate-spin" />
                <span className="animate-pulse">Whisper processing transcription...</span>
              </div>
            ) : activeCreation ? (
              activeCreation.status === "completed" ? (
                <p className="font-medium whitespace-pre-wrap">{activeCreation.resultImage || "Done."}</p>
              ) : (
                <span className="text-red-500 font-bold">Transcription failed: {activeCreation.error || "MuAPI error."}</span>
              )
            ) : (
              <div className="h-full flex flex-col items-center justify-center text-center text-secondary-text font-bold uppercase tracking-wider">
                <FaFileAlt className="text-2xl opacity-20 mb-2" />
                <span>Empty Transcript</span>
                <span className="text-[10px] text-secondary-text font-normal capitalize">Load an audio file and initiate the transcription engine.</span>
              </div>
            )}
          </div>
        </div>

        {activeCreation && activeCreation.status === "completed" && (
          <div className="border-t border-divider/40 pt-4 flex justify-end">
            <button
              onClick={() => handleDownloadTxt(activeCreation.resultImage)}
              className="bg-bg-page hover:bg-bg-card border border-divider px-4 py-2 text-xs text-primary font-bold rounded-full transition-all active:scale-95 flex items-center gap-2 cursor-pointer shadow-md"
            >
              <FaDownload size={10} />
              <span>Download Transcript (.txt)</span>
            </button>
          </div>
        )}
      </div>
    </div>
  );
}