Spaces:
Paused
Paused
File size: 5,103 Bytes
b1bbd1e | 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 | import React, { useState, useEffect } from "react";
import axios from "axios";
import AudioRecorder from "./AudioRecorder";
import ResponseDisplay from "./ResponseDisplay";
import "./App.css";
const App = () => {
const [response, setResponse] = useState("");
const [imageUrl, setImageUrl] = useState("");
const [loading, setLoading] = useState(false);
const [chatHistory, setChatHistory] = useState([]);
const [textInput, setTextInput] = useState("");
const [imageInput, setImageInput] = useState("");
useEffect(() => {
const fetchChatHistory = async () => {
try {
const res = await axios.get("http://localhost:8000/chat-history");
setChatHistory(res.data.chat_history);
} catch (error) {
console.error("Error fetching chat history:", error);
}
};
fetchChatHistory();
}, []);
const handleAudioSubmit = async (audioBlob) => {
const formData = new FormData();
formData.append("file", audioBlob, "audio.wav");
setLoading(true);
try {
const res = await axios.post("http://localhost:8000/upload", formData, {
headers: {
"Content-Type": "multipart/form-data",
},
});
if (res.data.image_url) {
setImageUrl(res.data.image_url);
setResponse("");
setChatHistory([
...chatHistory,
{ type: "image", content: res.data.image_url },
]);
} else {
setResponse(res.data.response);
setImageUrl("");
setChatHistory([
...chatHistory,
{ type: "text", content: res.data.response },
]);
}
} catch (error) {
console.error("Error sending audio to server:", error);
} finally {
setLoading(false);
}
};
const handleTextSubmit = async () => {
setLoading(true);
try {
const res = await axios.post("http://localhost:8000/text-input", new URLSearchParams({ prompt: textInput }));
if (res.data.image_url) {
setImageUrl(res.data.image_url);
setResponse("");
setChatHistory([
...chatHistory,
{ type: "image", content: res.data.image_url },
]);
} else {
setResponse(res.data.response);
setImageUrl("");
setChatHistory([
...chatHistory,
{ type: "text", content: res.data.response },
]);
}
} catch (error) {
console.error("Error sending text to server:", error);
} finally {
setLoading(false);
setTextInput("");
}
};
const handleImageUrlSubmit = async () => {
setLoading(true);
try {
const res = await axios.post("http://localhost:8000/image-url-input", new URLSearchParams({ url: imageInput, prompt: textInput }));
setResponse(res.data.response);
setImageUrl("");
setChatHistory([
...chatHistory,
{ type: "text", content: res.data.response },
]);
} catch (error) {
console.error("Error sending image URL to server:", error);
} finally {
setLoading(false);
setImageInput("");
setTextInput("");
}
};
const copyToClipboard = (text) => {
navigator.clipboard.writeText(text).then(() => {
alert("Text copied to clipboard!");
}).catch((error) => {
console.error("Error copying text: ", error);
});
};
const downloadImage = (url) => {
const element = document.createElement("a");
element.href = url;
element.download = "image.png";
document.body.appendChild(element);
element.click();
};
return (
<div className="container">
<h1>Ask a Question</h1>
<div className="input-container">
<input
type="text"
value={textInput}
onChange={(e) => setTextInput(e.target.value)}
placeholder="Type your question here..."
/>
<button onClick={handleTextSubmit}>Submit</button>
</div>
<div className="input-container">
<input
type="text"
value={imageInput}
onChange={(e) => setImageInput(e.target.value)}
placeholder="Enter image URL here..."
/>
<button onClick={handleImageUrlSubmit}>Submit Image URL</button>
</div>
<AudioRecorder onAudioSubmit={handleAudioSubmit} />
{loading && <p className="loading">Loading...</p>}
<div className="chat-window">
{chatHistory.map((entry, index) => (
<div key={index} className={`chat-message ${entry.type === "text" ? "assistant" : "user"}`}>
{entry.type === "text" ? (
<div>
<ResponseDisplay response={entry.content} />
<button onClick={() => copyToClipboard(entry.content)}>Copy Text</button>
</div>
) : (
<div>
<h3>Generated Image:</h3>
<img src={entry.content} alt="Generated" />
<button onClick={() => downloadImage(entry.content)}>Download Image</button>
</div>
)}
</div>
))}
</div>
</div>
);
};
export default App;
|