Spaces:
Runtime error
Runtime error
Update dialogue_generator.py
Browse files- dialogue_generator.py +21 -8
dialogue_generator.py
CHANGED
|
@@ -5,11 +5,14 @@ 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):
|
|
@@ -110,7 +113,7 @@ def create_image_dicts(encoded_images: List[str]) -> List[Dict[str, any]]:
|
|
| 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/
|
| 114 |
audio_dict = {
|
| 115 |
"role": "user",
|
| 116 |
"content": audio_blob
|
|
@@ -118,6 +121,13 @@ def create_audio_dict(encoded_audio: str) -> List[Dict[str, any]]:
|
|
| 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 = [
|
|
@@ -146,7 +156,7 @@ def process_dialogue(input_content: str, piece: ChessPiece, language: str, is_au
|
|
| 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)
|
|
@@ -182,14 +192,17 @@ def audio_to_base64(audio_path: str) -> str:
|
|
| 182 |
def generate_response_from_audio(dialogue: AudioDialogueInput) -> DialogueOutput:
|
| 183 |
piece = get_chess_piece(dialogue.IdPieza)
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
|
|
| 5 |
import google.generativeai as genai
|
| 6 |
from google.ai import generativelanguage as glm
|
| 7 |
import instructor
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
from models.chesspiece import ChessPiece
|
| 10 |
from personality_repository import ChessRepository, WhitePiece
|
| 11 |
from elevenlabs_texttospeech import text_to_speech
|
| 12 |
import pathlib
|
| 13 |
+
import re
|
| 14 |
|
| 15 |
+
load_dotenv()
|
| 16 |
|
| 17 |
# Endpoint Input
|
| 18 |
class TextDialogueInput(BaseModel):
|
|
|
|
| 113 |
|
| 114 |
def create_audio_dict(encoded_audio: str) -> List[Dict[str, any]]:
|
| 115 |
audio_data = base64.b64decode(encoded_audio)
|
| 116 |
+
audio_blob = glm.Blob(mime_type="audio/opus", data=audio_data)
|
| 117 |
audio_dict = {
|
| 118 |
"role": "user",
|
| 119 |
"content": audio_blob
|
|
|
|
| 121 |
|
| 122 |
return audio_dict
|
| 123 |
|
| 124 |
+
def extract_base64(data_uri: str) -> str:
|
| 125 |
+
"""Extract the base64 part from a Data URI."""
|
| 126 |
+
match = re.match(r'data:.*?;base64,(.+)', data_uri)
|
| 127 |
+
if match:
|
| 128 |
+
return match.group(1)
|
| 129 |
+
return data_uri # Return as-is if it doesn't match the pattern
|
| 130 |
+
|
| 131 |
def process_dialogue(input_content: str, piece: ChessPiece, language: str, is_audio: bool = False, images: List[str] = None) -> DialogueOutput:
|
| 132 |
system_prompt = generate_system_prompt(piece, language)
|
| 133 |
messages = [
|
|
|
|
| 156 |
chess_piece_llm_generation = llm_response
|
| 157 |
player_voice_attributes = None
|
| 158 |
|
| 159 |
+
# print(input_content)
|
| 160 |
print('='*50)
|
| 161 |
print(llm_response)
|
| 162 |
audio_file = generate_audio(text_response, piece)
|
|
|
|
| 192 |
def generate_response_from_audio(dialogue: AudioDialogueInput) -> DialogueOutput:
|
| 193 |
piece = get_chess_piece(dialogue.IdPieza)
|
| 194 |
|
| 195 |
+
print("AudioData received (first 100 characters):")
|
| 196 |
+
print(dialogue.AudioData[:100])
|
| 197 |
+
|
| 198 |
+
# Extract the base64 part from the Data URI
|
| 199 |
+
base64_audio = extract_base64(dialogue.AudioData)
|
| 200 |
+
|
| 201 |
+
print("Extracted base64 (first 100 characters):")
|
| 202 |
+
print(base64_audio[:100])
|
| 203 |
|
| 204 |
return process_dialogue(base64_audio, piece, dialogue.Language, is_audio=True)
|
| 205 |
+
|
|
|
|
| 206 |
def generate_response_from_webcam(dialogue: WebcamDialogueInput) -> DialogueOutput:
|
| 207 |
# Convert audio to base64 string
|
| 208 |
base64_audio = audio_to_base64(dialogue.AudioData)
|