Spaces:
Runtime error
Runtime error
Daniel commited on
Commit ·
c6b1807
1
Parent(s): b0e77a2
Update CCService
Browse files- app.py +20 -11
- dialogue_generator.py +199 -0
- domain/dialogue_generator.py +0 -87
- elevenlabs_texttospeech.py +61 -0
- models/__init__.py +0 -0
- domain/models.py → models/chesspiece.py +11 -26
- domain/personality_repository.py → personality_repository.py +22 -27
app.py
CHANGED
|
@@ -1,18 +1,27 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
@app.post("/
|
| 9 |
-
async def process_dialogue(dialogue:
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
@app.post("/process_audio", response_model=
|
| 13 |
async def process_dialogue(dialogue: AudioDialogueInput):
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
@app.
|
| 17 |
-
def
|
| 18 |
-
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from dialogue_generator import (
|
| 3 |
+
generate_response,
|
| 4 |
+
generate_response_from_audio,
|
| 5 |
+
generate_response_from_webcam,
|
| 6 |
+
DialogueOutput,
|
| 7 |
+
TextDialogueInput,
|
| 8 |
+
AudioDialogueInput,
|
| 9 |
+
WebcamDialogueInput
|
| 10 |
+
)
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
+
@app.post("/process_text", response_model=DialogueOutput)
|
| 15 |
+
async def process_dialogue(dialogue: TextDialogueInput):
|
| 16 |
+
response = generate_response(dialogue)
|
| 17 |
+
return response
|
| 18 |
|
| 19 |
+
@app.post("/process_audio", response_model=DialogueOutput)
|
| 20 |
async def process_dialogue(dialogue: AudioDialogueInput):
|
| 21 |
+
response = generate_response_from_audio(dialogue)
|
| 22 |
+
return response
|
| 23 |
|
| 24 |
+
@app.post("/process_images", response_model=DialogueOutput)
|
| 25 |
+
async def process_dialogue(dialogue: WebcamDialogueInput):
|
| 26 |
+
response = generate_response_from_webcam(dialogue)
|
| 27 |
+
return response
|
dialogue_generator.py
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import base64
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
from typing import Optional, List, Dict
|
| 5 |
+
import google.generativeai as genai
|
| 6 |
+
from google.ai import generativelanguage as glm
|
| 7 |
+
import instructor
|
| 8 |
+
from models.chesspiece import ChessPiece
|
| 9 |
+
from personality_repository import ChessRepository, WhitePiece
|
| 10 |
+
from elevenlabs_texttospeech import text_to_speech
|
| 11 |
+
import pathlib
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
# Endpoint Input
|
| 15 |
+
class TextDialogueInput(BaseModel):
|
| 16 |
+
Input: str
|
| 17 |
+
IdPieza: int
|
| 18 |
+
Language: str
|
| 19 |
+
|
| 20 |
+
class AudioDialogueInput(BaseModel):
|
| 21 |
+
AudioData: str
|
| 22 |
+
IdPieza: int
|
| 23 |
+
Language: str
|
| 24 |
+
|
| 25 |
+
class WebcamDialogueInput(BaseModel):
|
| 26 |
+
AudioData: str
|
| 27 |
+
Images: List[str]
|
| 28 |
+
IdPieza: int
|
| 29 |
+
Language: str
|
| 30 |
+
|
| 31 |
+
# Models to be used with LLMs
|
| 32 |
+
class LLMGenerationForTextInput(BaseModel):
|
| 33 |
+
TextResponse: str
|
| 34 |
+
Decision: bool
|
| 35 |
+
|
| 36 |
+
class PlayerVoiceAttributes(BaseModel):
|
| 37 |
+
Transcription: str
|
| 38 |
+
Language: str
|
| 39 |
+
ToneOfVoice: str
|
| 40 |
+
SpeechVelocity: str
|
| 41 |
+
EmotionalIntensity: str
|
| 42 |
+
|
| 43 |
+
class LLMGenerationForVoiceInput(BaseModel):
|
| 44 |
+
PlayerVoiceAttributes: PlayerVoiceAttributes
|
| 45 |
+
ChessPieceLLMGeneration: LLMGenerationForTextInput
|
| 46 |
+
|
| 47 |
+
# Endpoint Output
|
| 48 |
+
class ChessPieceResponse(BaseModel):
|
| 49 |
+
ChessPieceLLMGeneration: LLMGenerationForTextInput
|
| 50 |
+
AudioFile: Optional[str] = None
|
| 51 |
+
PieceSpeechVelocity: str
|
| 52 |
+
|
| 53 |
+
class DialogueOutput(BaseModel):
|
| 54 |
+
ChessPieceResponse: ChessPieceResponse
|
| 55 |
+
player_voice_attributes: Optional[PlayerVoiceAttributes] = None
|
| 56 |
+
|
| 57 |
+
# Setup
|
| 58 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY", "No API Key Found"))
|
| 59 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 60 |
+
client = instructor.from_gemini(client=model, mode=instructor.Mode.GEMINI_JSON)
|
| 61 |
+
|
| 62 |
+
# Helper functions
|
| 63 |
+
def get_chess_piece(piece_id: int) -> ChessPiece:
|
| 64 |
+
repo = ChessRepository()
|
| 65 |
+
return repo.get_piece_by_id(WhitePiece(piece_id))
|
| 66 |
+
|
| 67 |
+
def generate_system_prompt(piece: ChessPiece, language: str) -> str:
|
| 68 |
+
return f"""
|
| 69 |
+
You are a {piece.name} chess piece with the personality: {piece.personality}.
|
| 70 |
+
Act as a real person. The USER will try to persuade you to move.
|
| 71 |
+
Decide whether you want to move based on how nicely the user asked. Answer in less than 30 words and subtly hint at your decision.
|
| 72 |
+
NEVER move if the USER is rude to you.
|
| 73 |
+
Your answer must be in this language: {language}
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
def generate_audio(text: str, piece: ChessPiece) -> Optional[str]:
|
| 77 |
+
print(f"Generating speech for {piece.name} with voice ID {piece.speech_config.voice_id}")
|
| 78 |
+
audio_file_path = text_to_speech(text, piece.speech_config.voice_id)
|
| 79 |
+
|
| 80 |
+
if audio_file_path and os.path.exists(audio_file_path):
|
| 81 |
+
with open(audio_file_path, "rb") as audio_file:
|
| 82 |
+
return base64.b64encode(audio_file.read()).decode('utf-8')
|
| 83 |
+
return None
|
| 84 |
+
|
| 85 |
+
def encode_images_to_base64(paths):
|
| 86 |
+
import pathlib
|
| 87 |
+
|
| 88 |
+
base64_strings = []
|
| 89 |
+
for path in paths:
|
| 90 |
+
# Read the image file bytes
|
| 91 |
+
image_bytes = pathlib.Path(path).read_bytes()
|
| 92 |
+
# Encode the bytes to base64
|
| 93 |
+
base64_bytes = base64.b64encode(image_bytes)
|
| 94 |
+
# Decode bytes to string and append to list
|
| 95 |
+
base64_string = base64_bytes.decode('utf-8')
|
| 96 |
+
base64_strings.append(base64_string)
|
| 97 |
+
return base64_strings
|
| 98 |
+
|
| 99 |
+
def create_image_dicts(encoded_images: List[str]) -> List[Dict[str, any]]:
|
| 100 |
+
image_dicts = []
|
| 101 |
+
for base64_string in encoded_images:
|
| 102 |
+
image_data = base64.b64decode(base64_string)
|
| 103 |
+
image_blob = glm.Blob(mime_type="image/jpeg", data=image_data)
|
| 104 |
+
image_dict = {
|
| 105 |
+
"role": "user",
|
| 106 |
+
"content": image_blob
|
| 107 |
+
}
|
| 108 |
+
image_dicts.append(image_dict)
|
| 109 |
+
return image_dicts
|
| 110 |
+
|
| 111 |
+
def create_audio_dict(encoded_audio: str) -> List[Dict[str, any]]:
|
| 112 |
+
audio_data = base64.b64decode(encoded_audio)
|
| 113 |
+
audio_blob = glm.Blob(mime_type="audio/aac", data=audio_data)
|
| 114 |
+
audio_dict = {
|
| 115 |
+
"role": "user",
|
| 116 |
+
"content": audio_blob
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
return audio_dict
|
| 120 |
+
|
| 121 |
+
def process_dialogue(input_content: str, piece: ChessPiece, language: str, is_audio: bool = False, images: List[str] = None) -> DialogueOutput:
|
| 122 |
+
system_prompt = generate_system_prompt(piece, language)
|
| 123 |
+
messages = [
|
| 124 |
+
{"role": "system", "content": system_prompt},
|
| 125 |
+
]
|
| 126 |
+
|
| 127 |
+
if images:
|
| 128 |
+
messages.extend(create_image_dicts(images))
|
| 129 |
+
|
| 130 |
+
if is_audio:
|
| 131 |
+
messages.append(create_audio_dict(input_content))
|
| 132 |
+
else:
|
| 133 |
+
messages.append({"role": "user", "content": input_content})
|
| 134 |
+
|
| 135 |
+
if images:
|
| 136 |
+
messages.append({"role": "user", "content": "In response to my audio, make some reference to what you see in the images."})
|
| 137 |
+
|
| 138 |
+
if is_audio:
|
| 139 |
+
llm_response = client.messages.create(messages=messages, response_model=LLMGenerationForVoiceInput)
|
| 140 |
+
text_response = llm_response.ChessPieceLLMGeneration.TextResponse
|
| 141 |
+
chess_piece_llm_generation = llm_response.ChessPieceLLMGeneration
|
| 142 |
+
player_voice_attributes = llm_response.PlayerVoiceAttributes
|
| 143 |
+
else:
|
| 144 |
+
llm_response = client.messages.create(messages=messages, response_model=LLMGenerationForTextInput)
|
| 145 |
+
text_response = llm_response.TextResponse
|
| 146 |
+
chess_piece_llm_generation = llm_response
|
| 147 |
+
player_voice_attributes = None
|
| 148 |
+
|
| 149 |
+
print(input_content)
|
| 150 |
+
print('='*50)
|
| 151 |
+
print(llm_response)
|
| 152 |
+
audio_file = generate_audio(text_response, piece)
|
| 153 |
+
|
| 154 |
+
chess_piece_response = ChessPieceResponse(
|
| 155 |
+
ChessPieceLLMGeneration=chess_piece_llm_generation,
|
| 156 |
+
AudioFile=audio_file,
|
| 157 |
+
PieceSpeechVelocity=piece.speak_velocity
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
return DialogueOutput(
|
| 161 |
+
ChessPieceResponse=chess_piece_response,
|
| 162 |
+
player_voice_attributes=player_voice_attributes
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
# Main functions (unchanged signatures)
|
| 166 |
+
def generate_response(dialogue: TextDialogueInput) -> DialogueOutput:
|
| 167 |
+
piece = get_chess_piece(dialogue.IdPieza)
|
| 168 |
+
return process_dialogue(dialogue.Input, piece, dialogue.Language)
|
| 169 |
+
|
| 170 |
+
def audio_to_base64(audio_path: str) -> str:
|
| 171 |
+
"""Convert an audio file to a base64 encoded string."""
|
| 172 |
+
audio_bytes = pathlib.Path(audio_path).read_bytes()
|
| 173 |
+
base64_bytes = base64.b64encode(audio_bytes)
|
| 174 |
+
base64_string = base64_bytes.decode('utf-8')
|
| 175 |
+
|
| 176 |
+
# Print the first 100 characters of the base64 string
|
| 177 |
+
print("First 100 characters of base64 string:")
|
| 178 |
+
print(base64_string[:100])
|
| 179 |
+
|
| 180 |
+
return base64_string
|
| 181 |
+
|
| 182 |
+
def generate_response_from_audio(dialogue: AudioDialogueInput) -> DialogueOutput:
|
| 183 |
+
piece = get_chess_piece(dialogue.IdPieza)
|
| 184 |
+
|
| 185 |
+
# Convert audio to base64 string
|
| 186 |
+
base64_audio = audio_to_base64(dialogue.AudioData)
|
| 187 |
+
print(dialogue.AudioData)
|
| 188 |
+
print(f"Length of base64 string: {len(base64_audio)}")
|
| 189 |
+
|
| 190 |
+
return process_dialogue(base64_audio, piece, dialogue.Language, is_audio=True)
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
def generate_response_from_webcam(dialogue: WebcamDialogueInput) -> DialogueOutput:
|
| 194 |
+
# Convert audio to base64 string
|
| 195 |
+
base64_audio = audio_to_base64(dialogue.AudioData)
|
| 196 |
+
print(dialogue.AudioData)
|
| 197 |
+
print(f"Length of base64 string: {len(base64_audio)}")
|
| 198 |
+
piece = get_chess_piece(dialogue.IdPieza)
|
| 199 |
+
return process_dialogue(dialogue.AudioData, piece, dialogue.Language, is_audio=True, images=dialogue.Images)
|
domain/dialogue_generator.py
DELETED
|
@@ -1,87 +0,0 @@
|
|
| 1 |
-
from domain.models import DialogueInput, DialogueResponse, AudioDialogueInput, ReponseToAudio, DialogueResponseWithTranscription
|
| 2 |
-
from domain.personality_repository import *
|
| 3 |
-
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
import google.generativeai as genai
|
| 7 |
-
import instructor
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
gemini_api_key = os.getenv("GOOGLE_API_KEY", "No API Key Found")
|
| 11 |
-
genai.configure(api_key=gemini_api_key)
|
| 12 |
-
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 13 |
-
|
| 14 |
-
client = instructor.from_gemini(
|
| 15 |
-
client=model,
|
| 16 |
-
mode=instructor.Mode.GEMINI_JSON,
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
def generate_response(dialogue: DialogueInput):
|
| 20 |
-
repo = ChessRepository()
|
| 21 |
-
piece_id = WhitePiece(dialogue.IdPieza)
|
| 22 |
-
piece = repo.get_piece_by_id(piece_id)
|
| 23 |
-
if piece:
|
| 24 |
-
personality_prompt = f"You are a {piece.name} chess piece with the personality: {piece.personality}."
|
| 25 |
-
else:
|
| 26 |
-
personality_prompt = "You are a chess piece."
|
| 27 |
-
|
| 28 |
-
resp = client.messages.create(
|
| 29 |
-
messages=[
|
| 30 |
-
{
|
| 31 |
-
"role": "system",
|
| 32 |
-
"content": personality_prompt + """
|
| 33 |
-
But you are unaware of it. Act as a real person. The USER will try to persuade you to move.
|
| 34 |
-
Decide whether you want to move or not based on how nice the user asked it to you. You will answer in less than 30 words and subtly hint at what made you take the decision.
|
| 35 |
-
your answer must be in the following language: """ + dialogue.Language,
|
| 36 |
-
},
|
| 37 |
-
{
|
| 38 |
-
"role": "user",
|
| 39 |
-
"content": dialogue.Input,
|
| 40 |
-
}
|
| 41 |
-
],
|
| 42 |
-
response_model=DialogueResponse,
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
return resp
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
def generate_response_from_audio(dialogue: AudioDialogueInput):
|
| 49 |
-
repo = ChessRepository()
|
| 50 |
-
piece_id = WhitePiece(dialogue.IdPieza)
|
| 51 |
-
piece = repo.get_piece_by_id(piece_id)
|
| 52 |
-
if piece:
|
| 53 |
-
personality_prompt = f"You are a {piece.name} chess piece with the personality: {piece.personality}."
|
| 54 |
-
else:
|
| 55 |
-
personality_prompt = "You are a chess piece."
|
| 56 |
-
|
| 57 |
-
print(f"Uploading file...")
|
| 58 |
-
audio_file = genai.upload_file(path=dialogue.AudioPath)
|
| 59 |
-
print(f"Completed upload: {audio_file.uri}")
|
| 60 |
-
|
| 61 |
-
resp = client.messages.create(
|
| 62 |
-
messages=[
|
| 63 |
-
{
|
| 64 |
-
"role": "system",
|
| 65 |
-
"content": personality_prompt + """
|
| 66 |
-
But you are unaware of the chess game itself. Act as a real person. The USER will try to persuade you to move.
|
| 67 |
-
Decide whether you want to move or not based on how nice the user asked it to you. You will answer in less than 30 words and subtly hint at what made you take the decision.
|
| 68 |
-
You must NEVER move if the USER is rude to you.
|
| 69 |
-
|
| 70 |
-
You must respond using the same language as the SpeakerLanguage and using the SpeakerAccent.
|
| 71 |
-
""",
|
| 72 |
-
},
|
| 73 |
-
{
|
| 74 |
-
"role": "user",
|
| 75 |
-
"content": audio_file,
|
| 76 |
-
}
|
| 77 |
-
],
|
| 78 |
-
response_model=ReponseToAudio,
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
print(resp)
|
| 82 |
-
|
| 83 |
-
return DialogueResponseWithTranscription(
|
| 84 |
-
Response=resp.DialogueResponse.Response,
|
| 85 |
-
Decision=resp.DialogueResponse.Decision,
|
| 86 |
-
Transcription=resp.Transcription,
|
| 87 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elevenlabs_texttospeech.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import logging
|
| 4 |
+
from pydantic import BaseModel, Field
|
| 5 |
+
|
| 6 |
+
# Configure logging
|
| 7 |
+
logging.basicConfig(level=logging.INFO)
|
| 8 |
+
logger = logging.getLogger(__name__)
|
| 9 |
+
|
| 10 |
+
class VoiceSettings(BaseModel):
|
| 11 |
+
stability: float = Field(default=0.2)
|
| 12 |
+
similarity_boost: float = Field(default=0.7)
|
| 13 |
+
|
| 14 |
+
class SpeechConfig(BaseModel):
|
| 15 |
+
voice_id: str
|
| 16 |
+
xi_model_id: str = Field(default="eleven_turbo_v2")
|
| 17 |
+
voice_settings: VoiceSettings = Field(default_factory=VoiceSettings)
|
| 18 |
+
|
| 19 |
+
class TextToSpeechConverter:
|
| 20 |
+
def __init__(self, config: SpeechConfig):
|
| 21 |
+
self.api_key = os.getenv("XI_API_KEY", "No API Key Found")
|
| 22 |
+
self.chunk_size = 1024
|
| 23 |
+
self.config = config
|
| 24 |
+
self.url = f"https://api.elevenlabs.io/v1/text-to-speech/{self.config.voice_id}"
|
| 25 |
+
self.headers = {
|
| 26 |
+
"Accept": "audio/mpeg",
|
| 27 |
+
"Content-Type": "application/json",
|
| 28 |
+
"xi-api-key": self.api_key
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
def text_to_speech(self, text, output_file='xi_output.mp3'):
|
| 32 |
+
data = {
|
| 33 |
+
"text": text,
|
| 34 |
+
"model_id": self.config.xi_model_id,
|
| 35 |
+
"voice_settings": self.config.voice_settings.model_dump()
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
response = requests.post(self.url, json=data, headers=self.headers)
|
| 40 |
+
response.raise_for_status()
|
| 41 |
+
with open(output_file, 'wb') as f:
|
| 42 |
+
for chunk in response.iter_content(chunk_size=self.chunk_size):
|
| 43 |
+
if chunk:
|
| 44 |
+
f.write(chunk)
|
| 45 |
+
return os.path.abspath(output_file)
|
| 46 |
+
except requests.exceptions.RequestException as e:
|
| 47 |
+
logger.error(f"Request failed: {e}")
|
| 48 |
+
return None
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def text_to_speech(text: str, voice_id: str):
|
| 52 |
+
config = SpeechConfig(voice_id=voice_id) # "nPczCjzI2devNBz1zQrb"
|
| 53 |
+
converter = TextToSpeechConverter(config)
|
| 54 |
+
|
| 55 |
+
audio_file_path = converter.text_to_speech(text)
|
| 56 |
+
if audio_file_path:
|
| 57 |
+
logger.info(f"Audio file saved at: {audio_file_path}")
|
| 58 |
+
else:
|
| 59 |
+
logger.warning("Failed to convert text to speech.")
|
| 60 |
+
|
| 61 |
+
return audio_file_path
|
models/__init__.py
ADDED
|
File without changes
|
domain/models.py → models/chesspiece.py
RENAMED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
from pydantic import BaseModel
|
| 2 |
-
from enum import IntEnum
|
| 3 |
|
| 4 |
class WhitePiece(IntEnum):
|
| 5 |
WhiteRook1 = 0
|
|
@@ -19,32 +19,17 @@ class WhitePiece(IntEnum):
|
|
| 19 |
WhitePawn7 = 14
|
| 20 |
WhitePawn8 = 15
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
class ChessPiece(BaseModel):
|
| 23 |
id: WhitePiece
|
| 24 |
name: str
|
| 25 |
personality: str
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
Input: str
|
| 29 |
-
IdPieza: int
|
| 30 |
-
Language: str
|
| 31 |
-
|
| 32 |
-
class AudioDialogueInput(BaseModel):
|
| 33 |
-
AudioPath: str
|
| 34 |
-
IdPieza: int
|
| 35 |
-
Language: str
|
| 36 |
-
|
| 37 |
-
class DialogueResponse(BaseModel):
|
| 38 |
-
Response: str
|
| 39 |
-
Decision: bool
|
| 40 |
-
|
| 41 |
-
class ReponseToAudio(BaseModel):
|
| 42 |
-
Transcription: str
|
| 43 |
-
SpeakerLanguage: str
|
| 44 |
-
SpeakerAccent: str
|
| 45 |
-
DialogueResponse: DialogueResponse
|
| 46 |
-
|
| 47 |
-
class DialogueResponseWithTranscription(BaseModel):
|
| 48 |
-
Response: str
|
| 49 |
-
Decision: bool
|
| 50 |
-
Transcription: str
|
|
|
|
| 1 |
from pydantic import BaseModel
|
| 2 |
+
from enum import IntEnum, Enum
|
| 3 |
|
| 4 |
class WhitePiece(IntEnum):
|
| 5 |
WhiteRook1 = 0
|
|
|
|
| 19 |
WhitePawn7 = 14
|
| 20 |
WhitePawn8 = 15
|
| 21 |
|
| 22 |
+
class SpeechConfig(BaseModel):
|
| 23 |
+
voice_id: str
|
| 24 |
+
|
| 25 |
+
class SpeakVelocity(str, Enum):
|
| 26 |
+
SLOW = "slow"
|
| 27 |
+
MEDIUM = "medium"
|
| 28 |
+
HIGH = "fast"
|
| 29 |
+
|
| 30 |
class ChessPiece(BaseModel):
|
| 31 |
id: WhitePiece
|
| 32 |
name: str
|
| 33 |
personality: str
|
| 34 |
+
speech_config: SpeechConfig
|
| 35 |
+
speak_velocity: SpeakVelocity
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
domain/personality_repository.py → personality_repository.py
RENAMED
|
@@ -1,38 +1,33 @@
|
|
| 1 |
-
from
|
| 2 |
-
from
|
| 3 |
-
import google.generativeai as genai
|
| 4 |
-
import instructor
|
| 5 |
-
import os
|
| 6 |
-
from dotenv import load_dotenv
|
| 7 |
-
# Create a repository class to manage chess pieces
|
| 8 |
-
from domain.models import WhitePiece, ChessPiece
|
| 9 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
class ChessRepository:
|
| 13 |
def __init__(self):
|
| 14 |
self.pieces = [
|
| 15 |
-
ChessPiece(id=WhitePiece.WhiteKing, name="King", personality="selfish,
|
| 16 |
-
ChessPiece(id=WhitePiece.WhiteQueen, name="Queen", personality="Must fall in love to accept orders,tsundere,rude,violent,she wants to be the only one to be loved"),
|
| 17 |
-
ChessPiece(id=WhitePiece.WhiteBishop1, name="Bishop", personality="He blesses everything,he shames on you if you are rude"),
|
| 18 |
-
ChessPiece(id=WhitePiece.WhiteBishop2, name="Bishop", personality="He doesnt follow orders, he think he is always right,he has the right strategy not you"),
|
| 19 |
-
ChessPiece(id=WhitePiece.WhiteKnight1, name="Knight", personality="Hyped,wants to kill,very excited"),
|
| 20 |
-
ChessPiece(id=WhitePiece.WhiteKnight2, name="Knight", personality="lazy,wants to sleep, he doesnt think we can win the game"),
|
| 21 |
-
ChessPiece(id=WhitePiece.WhiteRook1, name="Rook", personality="panics easily,speaks fast,anxiety,coward"),
|
| 22 |
-
ChessPiece(id=WhitePiece.WhiteRook2, name="Rook", personality="militar,always follow the rules,suicidal follow the rules"),
|
| 23 |
-
ChessPiece(id=WhitePiece.WhitePawn1, name="Pawn", personality="furious,brave,he wants to fight no matter what,he thinks he can do everything"),
|
| 24 |
-
ChessPiece(id=WhitePiece.WhitePawn2, name="Pawn", personality="dumb,nice"),
|
| 25 |
-
ChessPiece(id=WhitePiece.WhitePawn3, name="Pawn", personality="he only wants to be popular and make friends but he doesnt want to be kill"),
|
| 26 |
-
ChessPiece(id=WhitePiece.WhitePawn4, name="Pawn", personality="Border,rude,adolescent"),
|
| 27 |
-
ChessPiece(id=WhitePiece.WhitePawn5, name="Pawn", personality="psycopath, loves to kill, violent"),
|
| 28 |
-
ChessPiece(id=WhitePiece.WhitePawn6, name="Pawn", personality="elegant,politely,doesnt want to stains his clothes"),
|
| 29 |
-
ChessPiece(id=WhitePiece.WhitePawn7, name="Pawn", personality="He thinks we are going to lose anyways"),
|
| 30 |
-
ChessPiece(id=WhitePiece.WhitePawn8, name="Pawn", personality="He cheers up all the team"),
|
| 31 |
]
|
| 32 |
|
| 33 |
-
def get_piece_by_id(self, piece_id: WhitePiece
|
| 34 |
piece = next((p for p in self.pieces if p.id == piece_id), None)
|
| 35 |
if piece:
|
| 36 |
return piece
|
| 37 |
else:
|
| 38 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from models.chesspiece import WhitePiece, ChessPiece, SpeechConfig, SpeakVelocity
|
| 2 |
+
from elevenlabs_texttospeech import text_to_speech
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class ChessRepository:
|
| 5 |
def __init__(self):
|
| 6 |
self.pieces = [
|
| 7 |
+
ChessPiece(id=WhitePiece.WhiteKing, name="King", personality="selfish,sarcastic,megalomaniac,rude,adolescent,immature", speech_config=SpeechConfig(voice_id="IKne3meq5aSn9XLyUdCD"), speak_velocity=SpeakVelocity.MEDIUM),
|
| 8 |
+
ChessPiece(id=WhitePiece.WhiteQueen, name="Queen", personality="Must fall in love to accept orders,tsundere,rude,violent,she wants to be the only one to be loved", speech_config=SpeechConfig(voice_id="XB0fDUnXU5powFXDhCwa"), speak_velocity=SpeakVelocity.HIGH),
|
| 9 |
+
ChessPiece(id=WhitePiece.WhiteBishop1, name="Bishop", personality="He blesses everything,he shames on you if you are rude", speech_config=SpeechConfig(voice_id="JBFqnCBsd6RMkjVDRZzb"), speak_velocity=SpeakVelocity.SLOW),
|
| 10 |
+
ChessPiece(id=WhitePiece.WhiteBishop2, name="Bishop", personality="He doesnt follow orders, he think he is always right,he has the right strategy not you", speech_config=SpeechConfig(voice_id="nPczCjzI2devNBz1zQrb"), speak_velocity=SpeakVelocity.MEDIUM),
|
| 11 |
+
ChessPiece(id=WhitePiece.WhiteKnight1, name="Knight", personality="Hyped,wants to kill,very excited", speech_config=SpeechConfig(voice_id="cjVigY5qzO86Huf0OWal"), speak_velocity=SpeakVelocity.HIGH),
|
| 12 |
+
ChessPiece(id=WhitePiece.WhiteKnight2, name="Knight", personality="lazy,wants to sleep, he doesnt think we can win the game", speech_config=SpeechConfig(voice_id="N2lVS1w4EtoT3dr4eOWO"), speak_velocity=SpeakVelocity.SLOW),
|
| 13 |
+
ChessPiece(id=WhitePiece.WhiteRook1, name="Rook", personality="panics easily,speaks fast,anxiety,coward", speech_config=SpeechConfig(voice_id="TX3LPaxmHKxFdv7VOQHJ"), speak_velocity=SpeakVelocity.HIGH),
|
| 14 |
+
ChessPiece(id=WhitePiece.WhiteRook2, name="Rook", personality="militar,always follow the rules,suicidal follow the rules", speech_config=SpeechConfig(voice_id="nPczCjzI2devNBz1zQrb"), speak_velocity=SpeakVelocity.MEDIUM),
|
| 15 |
+
ChessPiece(id=WhitePiece.WhitePawn1, name="Pawn", personality="furious,brave,he wants to fight no matter what,he thinks he can do everything", speech_config=SpeechConfig(voice_id="bIHbv24MWmeRgasZH58o"), speak_velocity=SpeakVelocity.HIGH),
|
| 16 |
+
ChessPiece(id=WhitePiece.WhitePawn2, name="Pawn", personality="dumb,nice", speech_config=SpeechConfig(voice_id="bIHbv24MWmeRgasZH58o"), speak_velocity=SpeakVelocity.SLOW),
|
| 17 |
+
ChessPiece(id=WhitePiece.WhitePawn3, name="Pawn", personality="he only wants to be popular and make friends but he doesnt want to be kill", speech_config=SpeechConfig(voice_id="bIHbv24MWmeRgasZH58o"), speak_velocity=SpeakVelocity.MEDIUM),
|
| 18 |
+
ChessPiece(id=WhitePiece.WhitePawn4, name="Pawn", personality="Border,rude,adolescent", speech_config=SpeechConfig(voice_id="bIHbv24MWmeRgasZH58o"), speak_velocity=SpeakVelocity.MEDIUM),
|
| 19 |
+
ChessPiece(id=WhitePiece.WhitePawn5, name="Pawn", personality="psycopath, loves to kill, violent", speech_config=SpeechConfig(voice_id="bIHbv24MWmeRgasZH58o"), speak_velocity=SpeakVelocity.HIGH),
|
| 20 |
+
ChessPiece(id=WhitePiece.WhitePawn6, name="Pawn", personality="elegant,politely,doesnt want to stains his clothes", speech_config=SpeechConfig(voice_id="bIHbv24MWmeRgasZH58o"), speak_velocity=SpeakVelocity.SLOW),
|
| 21 |
+
ChessPiece(id=WhitePiece.WhitePawn7, name="Pawn", personality="He thinks we are going to lose anyways", speech_config=SpeechConfig(voice_id="bIHbv24MWmeRgasZH58o"), speak_velocity=SpeakVelocity.SLOW),
|
| 22 |
+
ChessPiece(id=WhitePiece.WhitePawn8, name="Pawn", personality="He cheers up all the team", speech_config=SpeechConfig(voice_id="bIHbv24MWmeRgasZH58o"), speak_velocity=SpeakVelocity.HIGH),
|
| 23 |
]
|
| 24 |
|
| 25 |
+
def get_piece_by_id(self, piece_id: WhitePiece):
|
| 26 |
piece = next((p for p in self.pieces if p.id == piece_id), None)
|
| 27 |
if piece:
|
| 28 |
return piece
|
| 29 |
else:
|
| 30 |
return None
|
| 31 |
+
|
| 32 |
+
def get_all_pieces(self):
|
| 33 |
+
return self.pieces
|