-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.py
174 lines (135 loc) · 5.54 KB
/
text.py
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
171
172
173
174
# require deepgram-sdk==2.12.0
import asyncio
from dotenv import load_dotenv
import shutil
import subprocess
import requests
import time
import os
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.responses import StreamingResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import base64
import io
from pydantic import BaseModel
from langchain_core.prompts import ChatPromptTemplate
from langchain_groq import ChatGroq
from langchain_openai import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from langchain.prompts import (
ChatPromptTemplate,
MessagesPlaceholder,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from langchain.chains import LLMChain
from deepgram import Deepgram
load_dotenv()
app = FastAPI()
class LanguageModelProcessor:
def __init__(self):
self.llm = ChatGroq(temperature=0, model_name="llama3-8b-8192", groq_api_key=os.getenv("GROQ_API_KEY"))
# self.llm = ChatOpenAI(temperature=0, model_name="gpt-4-0125-preview", openai_api_key=os.getenv("OPENAI_API_KEY"))
# self.llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-0125", openai_api_key=os.getenv("OPENAI_API_KEY"))
self.memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
# Load the system prompt from a file
with open('system_prompt.txt', 'r') as file:
system_prompt = file.read().strip()
self.prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template(system_prompt),
MessagesPlaceholder(variable_name="chat_history"),
HumanMessagePromptTemplate.from_template("{text}")
])
self.conversation = LLMChain(
llm=self.llm,
prompt=self.prompt,
memory=self.memory
)
def process(self, text):
self.memory.chat_memory.add_user_message(text) # Add user message to memory
start_time = time.time()
# Go get the response from the LLM
response = self.conversation.invoke({"text": text})
end_time = time.time()
self.memory.chat_memory.add_ai_message(response['text']) # Add AI response to memory
elapsed_time = int((end_time - start_time) * 1000)
print(f"LLM ({elapsed_time}ms): {response['text']}")
return response['text']
class TextToSpeech:
# Set your Deepgram API Key and desired voice model
DG_API_KEY = os.getenv("DEEPGRAM_API_KEY")
MODEL_NAME = "aura-athena-en" # Example model name, change as needed
@staticmethod
def is_installed(lib_name: str) -> bool:
lib = shutil.which(lib_name)
return lib is not None
def speak(self, text):
if not self.is_installed("ffplay"):
raise ValueError("ffplay not found, necessary to stream audio.")
DEEPGRAM_URL = f"https://api.deepgram.com/v1/speak?model={self.MODEL_NAME}"
headers = {
"Authorization": f"Token {self.DG_API_KEY}",
"Content-Type": "application/json"
}
payload = {
"text": text
}
response = requests.post(DEEPGRAM_URL, headers=headers, json=payload, stream=True)
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail="TTS API error")
return response.content # Return the audio content as binary data
class TranscriptCollector:
def __init__(self):
self.reset()
def reset(self):
self.transcript_parts = []
def add_part(self, part):
self.transcript_parts.append(part)
def get_full_transcript(self):
return ' '.join(self.transcript_parts)
transcript_collector = TranscriptCollector()
async def transcribe_audio(audio_file: UploadFile):
try:
dg_client = Deepgram(os.getenv("DEEPGRAM_API_KEY"))
mime_type = audio_file.content_type
audio_data = await audio_file.read()
mime_type = audio_file.content_type
response = await dg_client.transcription.prerecorded(
{"buffer": audio_data, "mimetype": mime_type},
{"punctuate": True, "language": "en-US"}
)
transcript = response['results']['channels'][0]['alternatives'][0]['transcript']
return transcript
except Exception as e:
raise HTTPException(status_code=500, detail=f"Transcription error: {str(e)}")
# Specify allowed origins
origins = [
"http://localhost:3000", # Frontend URL
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins, # Adjust for specific origins in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class ConversationManager:
def __init__(self):
self.transcription_response = ""
self.llm = LanguageModelProcessor()
async def process_voice(self, audio_file: UploadFile):
self.transcription_response = await transcribe_audio(audio_file)
llm_response = self.llm.process(self.transcription_response)
return self.transcription_response, llm_response
conversation_manager = ConversationManager()
@app.post("/process_voice/")
async def process_voice(audio_file: UploadFile = File(...)):
"""Process voice input and return transcription, voice output, and LLM response"""
transcription, llm_response= await conversation_manager.process_voice(audio_file)
return {
"User": transcription,
"llm_response": llm_response # This will be a base64 encoded string
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)