1MR commited on
Commit
3246bd9
·
verified ·
1 Parent(s): 7764bd7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import os
3
+ import numpy as np
4
+ from fastapi import FastAPI, Form
5
+ from fastapi.responses import PlainTextResponse
6
+ from langchain_google_genai import ChatGoogleGenerativeAI
7
+ from pydub import AudioSegment
8
+
9
+ # ---------- CONFIG ----------
10
+ os.environ["GOOGLE_API_KEY"] = "AIzaSyD2DMFgcL0kWTQYhii8wseSHY3BRGWSebk"
11
+ llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash")
12
+
13
+ app = FastAPI()
14
+
15
+ # ---------- HELPERS ----------
16
+ def text_to_pcm_array(text: str) -> str:
17
+ """
18
+ Generate speech audio for `text`, convert to PCM unsigned 8-bit 8kHz mono,
19
+ and return as C-style array { 0,255, ... } for ESP32.
20
+ """
21
+
22
+ # 1. Generate response (you can replace with direct TTS if available)
23
+ response = llm.invoke(text).content
24
+ if not response:
25
+ response = "No response generated."
26
+
27
+ # 2. Save response as MP3 (dummy TTS workaround – here we just create a fake audio file)
28
+ # In practice, replace this with real TTS saving to 'response.mp3'
29
+ from gtts import gTTS
30
+ tts = gTTS(response)
31
+ tts.save("response.mp3")
32
+
33
+ # 3. Load audio with pydub
34
+ audio = AudioSegment.from_file("response.mp3")
35
+
36
+ # Convert to 8kHz, mono, 8-bit unsigned PCM
37
+ audio = audio.set_frame_rate(8000).set_channels(1).set_sample_width(1)
38
+
39
+ raw_data = audio.raw_data
40
+ samples = np.frombuffer(raw_data, dtype=np.uint8)
41
+
42
+ # 4. Convert to C-style array
43
+ c_array = "{ " + ",".join(map(str, samples)) + " }"
44
+ return c_array
45
+
46
+
47
+ # ---------- ENDPOINT ----------
48
+ @app.post("/send_message_recive_pcm", response_class=PlainTextResponse)
49
+ async def send_message_recive_pcm(message: str = Form(...)):
50
+ pcm_array = text_to_pcm_array(message)
51
+ return pcm_array