-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from futaringoto/fix/do_formatting
フォーマッタを実行した
- Loading branch information
Showing
10 changed files
with
99 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,28 @@ | ||
from contextlib import asynccontextmanager | ||
from typing import Union | ||
|
||
from fastapi import FastAPI | ||
from contextlib import asynccontextmanager | ||
|
||
from routers import raspi | ||
from utils.config import check_env_variables | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
check_env_variables() | ||
yield | ||
print("Shutting down...") | ||
|
||
|
||
app = FastAPI(lifespan=lifespan) | ||
app.include_router(raspi.router) | ||
|
||
|
||
@app.get("/") | ||
def read_root(): | ||
return {"Hello": "World"} | ||
|
||
|
||
@app.get("/items/{item_id}") | ||
def read_item(item_id: int, q: Union[str, None] = None): | ||
return {"item_id": item_id, "q": q} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
from openai import OpenAI | ||
|
||
from utils.config import get_openai_api_key | ||
|
||
OpenAI.api_key = get_openai_api_key() | ||
client = OpenAI() | ||
|
||
|
||
def generate_text(input_text: str) -> str: | ||
completion = client.chat.completions.create( | ||
model="gpt-4o-mini", | ||
messages=[ | ||
{"role": "system", "content": "テスト期間中の学生を親の視点から励ますような言葉を生成してください。日本語で40文字程度になるようにお願いします。"}, | ||
{"role": "user", "content": input_text} | ||
{ | ||
"role": "system", | ||
"content": "テスト期間中の学生を親の視点から励ますような言葉を生成してください。日本語で40文字程度になるようにお願いします。", | ||
}, | ||
{"role": "user", "content": input_text}, | ||
], | ||
) | ||
return completion.choices[0].message.content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,33 @@ | ||
import json | ||
import urllib.parse | ||
from typing import Any, Dict | ||
|
||
import httpx | ||
import urllib.parse | ||
|
||
from utils.config import get_voicevox_url | ||
|
||
url: str = get_voicevox_url() | ||
|
||
|
||
async def audio_query(text: str, speaker: int) -> Dict[str, Any]: | ||
params = {"text": text, "speaker": speaker} | ||
async with httpx.AsyncClient() as client: | ||
res = await client.post( | ||
urllib.parse.urljoin(url, "/audio_query"), | ||
params=params | ||
urllib.parse.urljoin(url, "/audio_query"), params=params | ||
) | ||
res.raise_for_status() | ||
query = res.json() | ||
return query | ||
|
||
|
||
async def synthesis(query: Dict[str, Any], speaker: int) -> bytes: | ||
async with httpx.AsyncClient(timeout=httpx.Timeout(20.0)) as client: | ||
res = await client.post( | ||
urllib.parse.urljoin(url, "/synthesis"), | ||
params={"speaker": speaker}, | ||
data=json.dumps(query), | ||
headers={"Content-Type": "application/json"} | ||
headers={"Content-Type": "application/json"}, | ||
) | ||
res.raise_for_status() | ||
content: bytes = res.content | ||
return content | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,15 @@ | ||
import httpx | ||
|
||
from utils.config import get_voicevox_api_key | ||
|
||
url: str = "https://deprecatedapis.tts.quest/v2/voicevox/audio/" | ||
API_KEY = get_voicevox_api_key() | ||
|
||
|
||
async def get_voicevox_audio(text: str, speaker: int) -> bytes: | ||
async with httpx.AsyncClient(timeout=httpx.Timeout(10.0)) as client: | ||
params = {"text": text, "speaker": speaker, "key": API_KEY} | ||
res = await client.post( | ||
url, | ||
params=params | ||
) | ||
res = await client.post(url, params=params) | ||
res.raise_for_status() | ||
content: bytes = res.content | ||
return content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
from openai import OpenAI | ||
|
||
from utils.config import get_openai_api_key | ||
|
||
OpenAI.api_key = get_openai_api_key() | ||
client = OpenAI() | ||
|
||
|
||
def speech2text(file_location: str) -> str: | ||
audio_file = open(file_location, "rb") | ||
transcription = client.audio.transcriptions.create( | ||
model="whisper-1", | ||
file=audio_file, | ||
response_format="json", | ||
language="ja" | ||
model="whisper-1", file=audio_file, response_format="json", language="ja" | ||
) | ||
return transcription |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,45 @@ | ||
import os | ||
|
||
|
||
def check_env_variables(): | ||
is_dev_mode:bool = get_is_dev_mode() | ||
env_vars:list[str] = [ | ||
is_dev_mode: bool = get_is_dev_mode() | ||
env_vars: list[str] = [ | ||
"OPENAI_API_KEY", | ||
"VOICEVOX_API_KEY", | ||
] | ||
env_vars_prod:list[str] = [ | ||
env_vars_prod: list[str] = [ | ||
"STORAGE_ACCOUNT_NAME", | ||
"SAS_TOKEN", | ||
] | ||
if not is_dev_mode: | ||
env_vars.extend(env_vars_prod) | ||
missing_vars = [var for var in env_vars if os.getenv(var) is None] | ||
if missing_vars: | ||
raise EnvironmentError(f"Missing environment variables: {', '.join(missing_vars)}") | ||
raise EnvironmentError( | ||
f"Missing environment variables: {', '.join(missing_vars)}" | ||
) | ||
|
||
|
||
def get_is_dev_mode() -> bool: | ||
is_dev_mode = os.getenv("IS_DEV_MODE") | ||
return int(is_dev_mode)==1 | ||
return int(is_dev_mode) == 1 | ||
|
||
|
||
def get_voicevox_url(): | ||
return os.getenv("VOICEVOX_URL") | ||
|
||
|
||
def get_openai_api_key(): | ||
return os.getenv("OPENAI_API_KEY") | ||
|
||
|
||
def get_storage_account_name(): | ||
return os.getenv("STORAGE_ACCOUNT_NAME") | ||
|
||
|
||
def get_sas_token(): | ||
return os.getenv("SAS_TOKEN") | ||
|
||
|
||
def get_voicevox_api_key(): | ||
return os.getenv("VOICEVOX_API_KEY") |
Oops, something went wrong.