Spaces:
Runtime error
Runtime error
Update TextGen/router.py
Browse files- TextGen/router.py +57 -57
TextGen/router.py
CHANGED
@@ -11,7 +11,7 @@ from langchain.prompts import PromptTemplate
|
|
11 |
from TextGen.suno import custom_generate_audio, get_audio_information,generate_lyrics
|
12 |
from TextGen.gemini import generate_story,place_objects,generate_map_markdown
|
13 |
#from TextGen.diffusion import generate_image
|
14 |
-
|
15 |
from langchain_google_genai import (
|
16 |
ChatGoogleGenerativeAI,
|
17 |
HarmBlockThreshold,
|
@@ -168,27 +168,27 @@ def inference_model(system_messsage, prompt):
|
|
168 |
|
169 |
@app.get("/", tags=["Home"])
|
170 |
def api_home(request: Request):
|
171 |
-
if request.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
172 |
-
|
173 |
return {'detail': 'Everchanging Quest backend, nothing to see here'}
|
174 |
|
175 |
@app.post("/api/generate", summary="Generate text from prompt", tags=["Generate"], response_model=Generate)
|
176 |
def inference(message: Message, request: Request):
|
177 |
-
if request.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
178 |
-
|
179 |
return generate_text(messages=message.messages, npc=message.npc)
|
180 |
|
181 |
@app.post("/invoke_model")
|
182 |
def story(prompt: Invoke,request: Request):
|
183 |
-
if request.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
184 |
-
|
185 |
return inference_model(system_messsage=prompt.system_prompt,prompt=prompt.message)
|
186 |
|
187 |
@app.post("/generate_level")
|
188 |
def placement(input: Rooms, request: Request):
|
189 |
print(request.headers)
|
190 |
-
if request.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
191 |
-
|
192 |
markdown_map=generate_map_markdown(input.rooms,input.room_of_interest,input.index_exit)
|
193 |
story=generate_story(input.possible_entities)
|
194 |
print(story)
|
@@ -198,8 +198,8 @@ def placement(input: Rooms, request: Request):
|
|
198 |
|
199 |
@app.post("/check_right_to_pass")
|
200 |
def check(input: Logs,request: Request):
|
201 |
-
if request.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
202 |
-
|
203 |
print(input.logs)
|
204 |
system_prompt="You are a game master in a roguelike. You previously decided of an objective for the player. You have to answer with YES or NO on wether the objective as been sucessfully completed. Be kind and flexible as the content of the game is AI generated and might not be feasible."
|
205 |
user_message=f"The objective was : {input.objective} and the player did the following actions : {input.logs}. Do you grant acess ? ONLY answer YES or NO"
|
@@ -261,62 +261,62 @@ async def generate_wav(message: VoiceMessage):
|
|
261 |
return 200
|
262 |
|
263 |
|
264 |
-
|
265 |
-
|
266 |
-
def generate_voice_eleven(request: Request, message: VoiceMessage = None):
|
267 |
-
if request.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
268 |
-
return 204
|
269 |
-
global Last_message # Declare Last_message as global
|
270 |
-
if message is None:
|
271 |
-
message = Last_message
|
272 |
-
else:
|
273 |
-
Last_message = message
|
274 |
-
|
275 |
-
def audio_stream():
|
276 |
-
this_voice_id=determine_elevenLav_voice_from_npc(message.npc, message.genre)
|
277 |
-
|
278 |
-
# Generate the audio stream from ElevenLabs
|
279 |
-
for chunk in Eleven_client.generate(text=message.input,
|
280 |
-
voice=Voice(
|
281 |
-
voice_id=this_voice_id,
|
282 |
-
settings=VoiceSettings(stability=0.71, similarity_boost=0.5, style=0.0, use_speaker_boost=True)
|
283 |
-
),
|
284 |
-
stream=True):
|
285 |
-
yield chunk
|
286 |
-
|
287 |
-
return StreamingResponse(audio_stream(), media_type="audio/mpeg")
|
288 |
-
#@app.get("/generate_voice_coqui", response_class=StreamingResponse)
|
289 |
-
#@app.post("/generate_voice_coqui", response_class=StreamingResponse)
|
290 |
-
#def generate_voice_coqui(message: VoiceMessage = None):
|
291 |
-
# global Last_message
|
292 |
# if message is None:
|
293 |
# message = Last_message
|
294 |
# else:
|
295 |
# Last_message = message
|
296 |
#
|
297 |
# def audio_stream():
|
298 |
-
#
|
299 |
-
#
|
300 |
-
# prompt=message.input,
|
301 |
-
# language=message.language,
|
302 |
-
# audio_file_pth=voice,
|
303 |
-
# mic_file_path=None,
|
304 |
-
# use_mic=False,
|
305 |
-
# voice_cleanup=False,
|
306 |
-
# no_lang_auto_detect=False,
|
307 |
-
# agree=True,
|
308 |
-
# )
|
309 |
# # Generate the audio stream from ElevenLabs
|
310 |
-
|
311 |
-
#
|
312 |
-
#
|
313 |
-
#
|
314 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
315 |
@app.post("/generate_song")
|
316 |
@app.get("/generate_song")
|
317 |
async def generate_song(request:SongRequest,httprequest: Request):
|
318 |
-
if httprequest.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
319 |
-
|
320 |
backstory=main_npc_system_prompts["Bard"]
|
321 |
print(backstory)
|
322 |
text=f"""the story is about a little girl in red hood adventuring in the dungeon behind the portal.{backstory} /n The user requested a song about : {request.prompt}"""
|
|
|
11 |
from TextGen.suno import custom_generate_audio, get_audio_information,generate_lyrics
|
12 |
from TextGen.gemini import generate_story,place_objects,generate_map_markdown
|
13 |
#from TextGen.diffusion import generate_image
|
14 |
+
from coqui import predict
|
15 |
from langchain_google_genai import (
|
16 |
ChatGoogleGenerativeAI,
|
17 |
HarmBlockThreshold,
|
|
|
168 |
|
169 |
@app.get("/", tags=["Home"])
|
170 |
def api_home(request: Request):
|
171 |
+
#if request.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
172 |
+
# return 204
|
173 |
return {'detail': 'Everchanging Quest backend, nothing to see here'}
|
174 |
|
175 |
@app.post("/api/generate", summary="Generate text from prompt", tags=["Generate"], response_model=Generate)
|
176 |
def inference(message: Message, request: Request):
|
177 |
+
#if request.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
178 |
+
# return 204
|
179 |
return generate_text(messages=message.messages, npc=message.npc)
|
180 |
|
181 |
@app.post("/invoke_model")
|
182 |
def story(prompt: Invoke,request: Request):
|
183 |
+
#if request.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
184 |
+
# return 204
|
185 |
return inference_model(system_messsage=prompt.system_prompt,prompt=prompt.message)
|
186 |
|
187 |
@app.post("/generate_level")
|
188 |
def placement(input: Rooms, request: Request):
|
189 |
print(request.headers)
|
190 |
+
#if request.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
191 |
+
# return 204
|
192 |
markdown_map=generate_map_markdown(input.rooms,input.room_of_interest,input.index_exit)
|
193 |
story=generate_story(input.possible_entities)
|
194 |
print(story)
|
|
|
198 |
|
199 |
@app.post("/check_right_to_pass")
|
200 |
def check(input: Logs,request: Request):
|
201 |
+
#if request.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
202 |
+
# return 204
|
203 |
print(input.logs)
|
204 |
system_prompt="You are a game master in a roguelike. You previously decided of an objective for the player. You have to answer with YES or NO on wether the objective as been sucessfully completed. Be kind and flexible as the content of the game is AI generated and might not be feasible."
|
205 |
user_message=f"The objective was : {input.objective} and the player did the following actions : {input.logs}. Do you grant acess ? ONLY answer YES or NO"
|
|
|
261 |
return 200
|
262 |
|
263 |
|
264 |
+
#@app.get("/generate_voice_eleven", response_class=StreamingResponse)
|
265 |
+
#@app.post("/generate_voice_eleven", response_class=StreamingResponse)
|
266 |
+
#def generate_voice_eleven(request: Request, message: VoiceMessage = None):
|
267 |
+
# if request.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
268 |
+
# return 204
|
269 |
+
# global Last_message # Declare Last_message as global
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
270 |
# if message is None:
|
271 |
# message = Last_message
|
272 |
# else:
|
273 |
# Last_message = message
|
274 |
#
|
275 |
# def audio_stream():
|
276 |
+
# this_voice_id=determine_elevenLav_voice_from_npc(message.npc, message.genre)
|
277 |
+
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
# # Generate the audio stream from ElevenLabs
|
279 |
+
### for chunk in Eleven_client.generate(text=message.input,
|
280 |
+
# voice=Voice(
|
281 |
+
# voice_id=this_voice_id,
|
282 |
+
# settings=VoiceSettings(stability=0.71, similarity_boost=0.5, style=0.0, use_speaker_boost=True)
|
283 |
+
# ),
|
284 |
+
# stream=True):
|
285 |
+
# yield chunk
|
286 |
+
|
287 |
+
# return StreamingResponse(audio_stream(), media_type="audio/mpeg")
|
288 |
+
@app.get("/generate_voice_coqui", response_class=StreamingResponse)
|
289 |
+
@app.post("/generate_voice_coqui", response_class=StreamingResponse)
|
290 |
+
def generate_voice_coqui(message: VoiceMessage = None):
|
291 |
+
global Last_message
|
292 |
+
if message is None:
|
293 |
+
message = Last_message
|
294 |
+
else:
|
295 |
+
Last_message = message
|
296 |
+
|
297 |
+
def audio_stream():
|
298 |
+
voice = determine_vocie_from_npc(message.npc, message.genre)
|
299 |
+
result = predict(
|
300 |
+
prompt=message.input,
|
301 |
+
language=message.language,
|
302 |
+
audio_file_pth=voice,
|
303 |
+
mic_file_path=None,
|
304 |
+
use_mic=False,
|
305 |
+
voice_cleanup=False,
|
306 |
+
no_lang_auto_detect=False,
|
307 |
+
agree=True,
|
308 |
+
)
|
309 |
+
# Generate the audio stream from ElevenLabs
|
310 |
+
for chunk in result:
|
311 |
+
print("received : ",chunk)
|
312 |
+
yield chunk#
|
313 |
+
|
314 |
+
return StreamingResponse(audio_stream(),media_type="audio/mpeg")
|
315 |
@app.post("/generate_song")
|
316 |
@app.get("/generate_song")
|
317 |
async def generate_song(request:SongRequest,httprequest: Request):
|
318 |
+
#if httprequest.headers.get("origin") != "https://jofthomas-everchanging-quest.static.hf.space":
|
319 |
+
# return 204
|
320 |
backstory=main_npc_system_prompts["Bard"]
|
321 |
print(backstory)
|
322 |
text=f"""the story is about a little girl in red hood adventuring in the dungeon behind the portal.{backstory} /n The user requested a song about : {request.prompt}"""
|