Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -1,57 +1,35 @@
|
|
1 |
-
from fastapi import FastAPI
|
2 |
-
|
3 |
-
import
|
|
|
4 |
|
5 |
-
# Set up logging
|
6 |
-
logging.basicConfig(level=logging.INFO)
|
7 |
-
logger = logging.getLogger(__name__)
|
8 |
-
|
9 |
-
# Initialize FastAPI app
|
10 |
app = FastAPI()
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
<script>
|
34 |
-
async function sendPrompt() {
|
35 |
-
const prompt = document.getElementById('prompt').value;
|
36 |
-
const responseDiv = document.getElementById('response');
|
37 |
-
responseDiv.innerHTML = 'Loading...';
|
38 |
-
try {
|
39 |
-
const resp = await puter.ai.chat(prompt, { model: 'grok-beta', stream: true });
|
40 |
-
responseDiv.innerHTML = '';
|
41 |
-
for await (const part of resp) {
|
42 |
-
responseDiv.innerHTML += part?.text?.replaceAll('\\n', '<br>') || '';
|
43 |
-
}
|
44 |
-
} catch (error) {
|
45 |
-
responseDiv.innerHTML = 'Error: ' + error.message;
|
46 |
-
}
|
47 |
-
}
|
48 |
-
</script>
|
49 |
-
</body>
|
50 |
-
</html>
|
51 |
-
"""
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
2 |
+
import speech_recognition as sr
|
3 |
+
import os
|
4 |
+
import uuid
|
5 |
|
|
|
|
|
|
|
|
|
|
|
6 |
app = FastAPI()
|
7 |
|
8 |
+
UPLOAD_DIR = "uploads"
|
9 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
10 |
+
|
11 |
+
@app.post("/voice-to-text")
|
12 |
+
async def voice_to_text(file: UploadFile = File(...)):
|
13 |
+
# Check for .wav file
|
14 |
+
if not file.filename.endswith(".wav"):
|
15 |
+
raise HTTPException(status_code=400, detail="Only .wav files are supported")
|
16 |
+
|
17 |
+
# Save uploaded file
|
18 |
+
file_path = os.path.join(UPLOAD_DIR, f"{uuid.uuid4()}.wav")
|
19 |
+
with open(file_path, "wb") as f:
|
20 |
+
f.write(await file.read())
|
21 |
+
|
22 |
+
# Speech recognition
|
23 |
+
recognizer = sr.Recognizer()
|
24 |
+
try:
|
25 |
+
with sr.AudioFile(file_path) as source:
|
26 |
+
audio_data = recognizer.record(source)
|
27 |
+
recognized_text = recognizer.recognize_google(audio_data)
|
28 |
+
return {"recognized_text": recognized_text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
except sr.UnknownValueError:
|
31 |
+
raise HTTPException(status_code=400, detail="Could not understand audio")
|
32 |
+
except sr.RequestError as e:
|
33 |
+
raise HTTPException(status_code=500, detail=f"Speech API error: {e}")
|
34 |
+
finally:
|
35 |
+
os.remove(file_path)
|