Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- .firebaserc +5 -0
- .gitignore +162 -0
- README.md +3 -9
- firebase.json +23 -0
- functions/.gitignore +0 -0
- functions/backend/__init__.py +0 -0
- functions/backend/ai_processing/__init__.py +0 -0
- functions/backend/ai_processing/gpt.py +38 -0
- functions/backend/ai_processing/process_audio.py +33 -0
- functions/backend/ai_processing/prompt.py +28 -0
- functions/backend/ai_processing/transcription.py +35 -0
- functions/main.py +67 -0
- functions/requirements.txt +73 -0
- main.py +74 -0
.firebaserc
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"projects": {
|
| 3 |
+
"default": "ai-language-detector"
|
| 4 |
+
}
|
| 5 |
+
}
|
.gitignore
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
share/python-wheels/
|
| 24 |
+
*.egg-info/
|
| 25 |
+
.installed.cfg
|
| 26 |
+
*.egg
|
| 27 |
+
MANIFEST
|
| 28 |
+
|
| 29 |
+
# PyInstaller
|
| 30 |
+
# Usually these files are written by a python script from a template
|
| 31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 32 |
+
*.manifest
|
| 33 |
+
*.spec
|
| 34 |
+
|
| 35 |
+
# Installer logs
|
| 36 |
+
pip-log.txt
|
| 37 |
+
pip-delete-this-directory.txt
|
| 38 |
+
|
| 39 |
+
# Unit test / coverage reports
|
| 40 |
+
htmlcov/
|
| 41 |
+
.tox/
|
| 42 |
+
.nox/
|
| 43 |
+
.coverage
|
| 44 |
+
.coverage.*
|
| 45 |
+
.cache
|
| 46 |
+
nosetests.xml
|
| 47 |
+
coverage.xml
|
| 48 |
+
*.cover
|
| 49 |
+
*.py,cover
|
| 50 |
+
.hypothesis/
|
| 51 |
+
.pytest_cache/
|
| 52 |
+
cover/
|
| 53 |
+
|
| 54 |
+
# Translations
|
| 55 |
+
*.mo
|
| 56 |
+
*.pot
|
| 57 |
+
|
| 58 |
+
# Django stuff:
|
| 59 |
+
*.log
|
| 60 |
+
local_settings.py
|
| 61 |
+
db.sqlite3
|
| 62 |
+
db.sqlite3-journal
|
| 63 |
+
|
| 64 |
+
# Flask stuff:
|
| 65 |
+
instance/
|
| 66 |
+
.webassets-cache
|
| 67 |
+
|
| 68 |
+
# Scrapy stuff:
|
| 69 |
+
.scrapy
|
| 70 |
+
|
| 71 |
+
# Sphinx documentation
|
| 72 |
+
docs/_build/
|
| 73 |
+
|
| 74 |
+
# PyBuilder
|
| 75 |
+
.pybuilder/
|
| 76 |
+
target/
|
| 77 |
+
|
| 78 |
+
# Jupyter Notebook
|
| 79 |
+
.ipynb_checkpoints
|
| 80 |
+
|
| 81 |
+
# IPython
|
| 82 |
+
profile_default/
|
| 83 |
+
ipython_config.py
|
| 84 |
+
|
| 85 |
+
# pyenv
|
| 86 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 88 |
+
# .python-version
|
| 89 |
+
|
| 90 |
+
# pipenv
|
| 91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 94 |
+
# install all needed dependencies.
|
| 95 |
+
#Pipfile.lock
|
| 96 |
+
|
| 97 |
+
# poetry
|
| 98 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 100 |
+
# commonly ignored for libraries.
|
| 101 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 102 |
+
#poetry.lock
|
| 103 |
+
|
| 104 |
+
# pdm
|
| 105 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 106 |
+
#pdm.lock
|
| 107 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
| 108 |
+
# in version control.
|
| 109 |
+
# https://pdm.fming.dev/#use-with-ide
|
| 110 |
+
.pdm.toml
|
| 111 |
+
|
| 112 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 113 |
+
__pypackages__/
|
| 114 |
+
|
| 115 |
+
# Celery stuff
|
| 116 |
+
celerybeat-schedule
|
| 117 |
+
celerybeat.pid
|
| 118 |
+
|
| 119 |
+
# SageMath parsed files
|
| 120 |
+
*.sage.py
|
| 121 |
+
|
| 122 |
+
# Environments
|
| 123 |
+
.env
|
| 124 |
+
.venv
|
| 125 |
+
env/
|
| 126 |
+
venv/
|
| 127 |
+
ENV/
|
| 128 |
+
env.bak/
|
| 129 |
+
venv.bak/
|
| 130 |
+
|
| 131 |
+
# Spyder project settings
|
| 132 |
+
.spyderproject
|
| 133 |
+
.spyproject
|
| 134 |
+
|
| 135 |
+
# Rope project settings
|
| 136 |
+
.ropeproject
|
| 137 |
+
|
| 138 |
+
# mkdocs documentation
|
| 139 |
+
/site
|
| 140 |
+
|
| 141 |
+
# mypy
|
| 142 |
+
.mypy_cache/
|
| 143 |
+
.dmypy.json
|
| 144 |
+
dmypy.json
|
| 145 |
+
|
| 146 |
+
# Pyre type checker
|
| 147 |
+
.pyre/
|
| 148 |
+
|
| 149 |
+
# pytype static type analyzer
|
| 150 |
+
.pytype/
|
| 151 |
+
|
| 152 |
+
# Cython debug symbols
|
| 153 |
+
cython_debug/
|
| 154 |
+
|
| 155 |
+
# PyCharm
|
| 156 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 157 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 158 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 159 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 160 |
+
#.idea/
|
| 161 |
+
flagged
|
| 162 |
+
audio
|
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
|
| 4 |
-
colorFrom: gray
|
| 5 |
-
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 4.19.
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: AI_Language_Detector
|
| 3 |
+
app_file: main.py
|
|
|
|
|
|
|
| 4 |
sdk: gradio
|
| 5 |
+
sdk_version: 4.19.1
|
|
|
|
|
|
|
| 6 |
---
|
|
|
|
|
|
firebase.json
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"functions": [
|
| 3 |
+
{
|
| 4 |
+
"source": "functions",
|
| 5 |
+
"codebase": "default",
|
| 6 |
+
"ignore": [
|
| 7 |
+
"venv",
|
| 8 |
+
".git",
|
| 9 |
+
"firebase-debug.log",
|
| 10 |
+
"firebase-debug.*.log"
|
| 11 |
+
]
|
| 12 |
+
}
|
| 13 |
+
],
|
| 14 |
+
"emulators": {
|
| 15 |
+
"functions": {
|
| 16 |
+
"port": 5001
|
| 17 |
+
},
|
| 18 |
+
"ui": {
|
| 19 |
+
"enabled": true
|
| 20 |
+
},
|
| 21 |
+
"singleProjectMode": true
|
| 22 |
+
}
|
| 23 |
+
}
|
functions/.gitignore
ADDED
|
File without changes
|
functions/backend/__init__.py
ADDED
|
File without changes
|
functions/backend/ai_processing/__init__.py
ADDED
|
File without changes
|
functions/backend/ai_processing/gpt.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
from .prompt import SYSTEM_PROMPT
|
| 6 |
+
|
| 7 |
+
# Load your API key from an environment variable or secret management service
|
| 8 |
+
|
| 9 |
+
def ai_language_detector(input_text:str) -> str | None:
|
| 10 |
+
try:
|
| 11 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
| 12 |
+
client = openai.OpenAI(api_key=api_key)
|
| 13 |
+
|
| 14 |
+
completion = client.chat.completions.create(
|
| 15 |
+
response_format={"type": "json_object"},
|
| 16 |
+
model="gpt-4-0125-preview",
|
| 17 |
+
messages=[
|
| 18 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 19 |
+
{"role": "user", "content": input_text},
|
| 20 |
+
],
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
content = json.loads(completion.choices[0].message.content or "{}")
|
| 25 |
+
if "language" not in content:
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
return content["language"]
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"An error occurred: {e}")
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
# Example usage:
|
| 38 |
+
print(ai_language_detector("hola como estas?"))
|
functions/backend/ai_processing/process_audio.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from backend.ai_processing.transcription import transcribe_audio_file_with_language
|
| 3 |
+
from backend.ai_processing.gpt import ai_language_detector
|
| 4 |
+
|
| 5 |
+
def process_audio(file_path: str) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Processes the audio file by transcribing it and detecting its language.
|
| 8 |
+
|
| 9 |
+
- Takes the path to an audio file.
|
| 10 |
+
- Ensures the file exists.
|
| 11 |
+
- Uses the transcribe_audio_file_with_language function to transcribe the audio.
|
| 12 |
+
- Detects the language of the transcription.
|
| 13 |
+
- Raises FileNotFoundError if the file does not exist.
|
| 14 |
+
- Raises ValueError if the language cannot be detected (None).
|
| 15 |
+
- Returns the detected language as a string.
|
| 16 |
+
"""
|
| 17 |
+
if not os.path.exists(file_path):
|
| 18 |
+
raise FileNotFoundError(f"File {file_path} not found.")
|
| 19 |
+
|
| 20 |
+
transcription = transcribe_audio_file_with_language(file_path)
|
| 21 |
+
predicted_language = ai_language_detector(transcription)
|
| 22 |
+
|
| 23 |
+
if predicted_language is None:
|
| 24 |
+
raise ValueError("Language could not be detected.")
|
| 25 |
+
|
| 26 |
+
return predicted_language
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
# Example usage
|
| 31 |
+
file_path = "path/to/audio/file.wav"
|
| 32 |
+
transcription = process_audio(file_path)
|
| 33 |
+
print(transcription)
|
functions/backend/ai_processing/prompt.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
SYSTEM_PROMPT = """
|
| 2 |
+
you are a language detection system.
|
| 3 |
+
|
| 4 |
+
you will be given a string of text. Your task is to determine the language of the text.
|
| 5 |
+
|
| 6 |
+
you will only return a json object with the language of the text and language code in parentheses.
|
| 7 |
+
|
| 8 |
+
examples:
|
| 9 |
+
|
| 10 |
+
{
|
| 11 |
+
"language": "🇺🇸 ️English (en) "
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
or
|
| 15 |
+
|
| 16 |
+
{
|
| 17 |
+
"language": "🇫🇷 ️French (fr)"
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
or
|
| 21 |
+
|
| 22 |
+
{
|
| 23 |
+
"language": "🇪🇸 ️Spanish (es)"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
"""
|
| 28 |
+
|
functions/backend/ai_processing/transcription.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import logging
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# Transcribe audio file using OpenAI's Whisper model
|
| 6 |
+
|
| 7 |
+
def transcribe_audio_file_with_language(file_path: str) -> str:
|
| 8 |
+
"""
|
| 9 |
+
- Transcribe audio file using OpenAI's Whisper model
|
| 10 |
+
- Returns transcription as a string
|
| 11 |
+
- default language is English
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
# Initialize OpenAI client with API key
|
| 15 |
+
openai_api_key = os.environ.get("OPENAI_API_KEY")
|
| 16 |
+
client = OpenAI(api_key=openai_api_key)
|
| 17 |
+
|
| 18 |
+
logging.info(f"Transcribing audio file: {file_path}")
|
| 19 |
+
|
| 20 |
+
with open(file_path, "rb") as audio_file:
|
| 21 |
+
# Request transcription using Whisper model
|
| 22 |
+
transcript = client.audio.transcriptions.create(
|
| 23 |
+
model="whisper-1", file=audio_file, prompt="Transcribe this audio file and best guess the language."
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Get the transcription text
|
| 27 |
+
transcription = transcript.text
|
| 28 |
+
|
| 29 |
+
return transcription
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
# Test the function
|
| 33 |
+
file_path = "/Users/hassen/LocalDev/PYTHON_PROJECTS/GPTGenProjects/languageDetector/audio/recorded_audio.wav"
|
| 34 |
+
transcription = transcribe_audio_file_with_language(file_path)
|
| 35 |
+
print(transcription)
|
functions/main.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Welcome to Cloud Functions for Firebase for Python!
|
| 2 |
+
# To get started, simply uncomment the below code or create your own.
|
| 3 |
+
# Deploy with `firebase deploy`
|
| 4 |
+
|
| 5 |
+
from firebase_functions import https_fn
|
| 6 |
+
from firebase_admin import initialize_app
|
| 7 |
+
import io # Ensure this import is added to handle BytesIO
|
| 8 |
+
import tempfile
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
from backend.ai_processing.gpt import ai_language_detector
|
| 12 |
+
from backend.ai_processing.transcription import transcribe_audio_file_with_language
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
initialize_app()
|
| 16 |
+
|
| 17 |
+
@https_fn.on_request()
|
| 18 |
+
def detect_language(req: https_fn.Request) -> https_fn.Response:
|
| 19 |
+
"""
|
| 20 |
+
Cloud Function to detect language from an uploaded audio file.
|
| 21 |
+
|
| 22 |
+
- Accepts POST requests with an audio file.
|
| 23 |
+
- Validates the file format (assumes WAV for this example).
|
| 24 |
+
- Reads the audio file content.
|
| 25 |
+
- Transcribes the audio content and detects the language.
|
| 26 |
+
- Returns the detected language as JSON.
|
| 27 |
+
"""
|
| 28 |
+
if req.method != 'POST':
|
| 29 |
+
return https_fn.Response("Method Not Allowed", status=405)
|
| 30 |
+
|
| 31 |
+
# Check if there is a file in the request
|
| 32 |
+
if 'file' not in req.files:
|
| 33 |
+
return https_fn.Response("No file part in the request", status=400)
|
| 34 |
+
|
| 35 |
+
file = req.files['file']
|
| 36 |
+
|
| 37 |
+
# Enforce a specific file format here, e.g., WAV
|
| 38 |
+
if not file or not file.filename or not file.filename.endswith(('.mp3', '.mp4', '.mpeg', '.mpga', '.m4a', '.wav', '.webm')):
|
| 39 |
+
return https_fn.Response("File format not supported.", status=400)
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
# Save the file to a temporary file
|
| 43 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as tmp_file:
|
| 44 |
+
file_content = file.read() # Read file content
|
| 45 |
+
tmp_file.write(file_content) # Write content to temporary file
|
| 46 |
+
tmp_file_path = tmp_file.name # Get the path of the temporary file
|
| 47 |
+
|
| 48 |
+
# Process the audio file and detect language
|
| 49 |
+
transcription = transcribe_audio_file_with_language(tmp_file_path)
|
| 50 |
+
|
| 51 |
+
# Clean up the temporary file
|
| 52 |
+
os.remove(tmp_file_path)
|
| 53 |
+
|
| 54 |
+
# Detect the language from the transcription
|
| 55 |
+
predicted_language = ai_language_detector(transcription)
|
| 56 |
+
|
| 57 |
+
if predicted_language is None:
|
| 58 |
+
raise ValueError("Language could not be detected.")
|
| 59 |
+
|
| 60 |
+
# Return the detected language as JSON
|
| 61 |
+
return https_fn.Response(f"{predicted_language}", status=200, mimetype='application/json')
|
| 62 |
+
except ValueError as e:
|
| 63 |
+
# Handle specific errors, e.g., language not detected
|
| 64 |
+
return https_fn.Response(f"Error: {str(e)}", status=400)
|
| 65 |
+
except Exception as e:
|
| 66 |
+
# Handle errors (e.g., transcription failure, unsupported audio format)
|
| 67 |
+
return https_fn.Response(f"Error processing the audio file: {str(e)}", status=500)
|
functions/requirements.txt
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
firebase_functions~=0.1.0
|
| 2 |
+
aiofiles==23.2.1
|
| 3 |
+
altair==5.2.0
|
| 4 |
+
annotated-types==0.6.0
|
| 5 |
+
anyio==4.3.0
|
| 6 |
+
attrs==23.2.0
|
| 7 |
+
certifi==2024.2.2
|
| 8 |
+
cffi==1.16.0
|
| 9 |
+
charset-normalizer==3.3.2
|
| 10 |
+
click==8.1.7
|
| 11 |
+
colorama==0.4.6
|
| 12 |
+
contourpy==1.2.0
|
| 13 |
+
cycler==0.12.1
|
| 14 |
+
distro==1.9.0
|
| 15 |
+
fastapi==0.109.2
|
| 16 |
+
ffmpy==0.3.2
|
| 17 |
+
filelock==3.13.1
|
| 18 |
+
fonttools==4.49.0
|
| 19 |
+
fsspec==2024.2.0
|
| 20 |
+
gradio==4.19.1
|
| 21 |
+
gradio_client==0.10.0
|
| 22 |
+
h11==0.14.0
|
| 23 |
+
httpcore==1.0.3
|
| 24 |
+
httpx==0.26.0
|
| 25 |
+
huggingface-hub==0.20.3
|
| 26 |
+
idna==3.6
|
| 27 |
+
importlib-resources==6.1.1
|
| 28 |
+
Jinja2==3.1.3
|
| 29 |
+
jsonschema==4.21.1
|
| 30 |
+
jsonschema-specifications==2023.12.1
|
| 31 |
+
kiwisolver==1.4.5
|
| 32 |
+
markdown-it-py==3.0.0
|
| 33 |
+
MarkupSafe==2.1.5
|
| 34 |
+
matplotlib==3.8.3
|
| 35 |
+
mdurl==0.1.2
|
| 36 |
+
numpy==1.26.4
|
| 37 |
+
openai==1.12.0
|
| 38 |
+
orjson==3.9.14
|
| 39 |
+
packaging==23.2
|
| 40 |
+
pandas==2.2.0
|
| 41 |
+
pillow==10.2.0
|
| 42 |
+
pycparser==2.21
|
| 43 |
+
pydantic==2.6.1
|
| 44 |
+
pydantic_core==2.16.2
|
| 45 |
+
pydub==0.25.1
|
| 46 |
+
Pygments==2.17.2
|
| 47 |
+
pyparsing==3.1.1
|
| 48 |
+
python-dateutil==2.8.2
|
| 49 |
+
python-multipart==0.0.9
|
| 50 |
+
pytz==2024.1
|
| 51 |
+
PyYAML==6.0.1
|
| 52 |
+
referencing==0.33.0
|
| 53 |
+
requests==2.31.0
|
| 54 |
+
rich==13.7.0
|
| 55 |
+
rpds-py==0.18.0
|
| 56 |
+
ruff==0.2.2
|
| 57 |
+
scipy==1.12.0
|
| 58 |
+
semantic-version==2.10.0
|
| 59 |
+
shellingham==1.5.4
|
| 60 |
+
six==1.16.0
|
| 61 |
+
sniffio==1.3.0
|
| 62 |
+
sounddevice==0.4.6
|
| 63 |
+
soundfile==0.12.1
|
| 64 |
+
starlette==0.36.3
|
| 65 |
+
tomlkit==0.12.0
|
| 66 |
+
toolz==0.12.1
|
| 67 |
+
tqdm==4.66.2
|
| 68 |
+
typer==0.9.0
|
| 69 |
+
typing_extensions==4.9.0
|
| 70 |
+
tzdata==2024.1
|
| 71 |
+
urllib3==2.2.1
|
| 72 |
+
uvicorn==0.27.1
|
| 73 |
+
websockets==11.0.3
|
main.py
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from scipy.io.wavfile import write
|
| 4 |
+
import os
|
| 5 |
+
import requests # Import requests library
|
| 6 |
+
|
| 7 |
+
# from backend.ai_processing.process_audio import process_audio # Import the process_audio function
|
| 8 |
+
|
| 9 |
+
# Define constants
|
| 10 |
+
SAMPLE_RATE = 44100 # Sample rate in Hz
|
| 11 |
+
AUDIO_DIR = 'audio' # Directory to save audio files
|
| 12 |
+
|
| 13 |
+
# Ensure the audio directory exists
|
| 14 |
+
if not os.path.exists(AUDIO_DIR):
|
| 15 |
+
os.makedirs(AUDIO_DIR)
|
| 16 |
+
|
| 17 |
+
def save_and_transcribe_audio(recording, filename='recording.wav'):
|
| 18 |
+
"""
|
| 19 |
+
Saves the recorded audio to a file and sends it to an external API for transcription and language detection.
|
| 20 |
+
|
| 21 |
+
- Extracts sample rate and audio data from the recording.
|
| 22 |
+
- Scales the audio data to 16-bit integers.
|
| 23 |
+
- Saves the audio data as a WAV file.
|
| 24 |
+
- Sends the saved audio file to an external API for transcription and language detection.
|
| 25 |
+
- Returns the response from the API.
|
| 26 |
+
"""
|
| 27 |
+
# Ensure recording is not None
|
| 28 |
+
try:
|
| 29 |
+
# Attempt to extract sample rate and audio data
|
| 30 |
+
sample_rate, audio_data = recording
|
| 31 |
+
except TypeError:
|
| 32 |
+
raise ValueError("Invalid recording: Expected a tuple with sample rate and audio data, got None.")
|
| 33 |
+
except ValueError:
|
| 34 |
+
raise ValueError("Invalid recording: Expected a tuple with sample rate and audio data.")
|
| 35 |
+
|
| 36 |
+
# Ensure audio_data is a NumPy array
|
| 37 |
+
audio_data = np.array(audio_data)
|
| 38 |
+
|
| 39 |
+
# Scale the audio data to 16-bit integers
|
| 40 |
+
scaled = np.int16(audio_data/np.max(np.abs(audio_data)) * 32767)
|
| 41 |
+
|
| 42 |
+
# Save as WAV file
|
| 43 |
+
filepath = os.path.join(AUDIO_DIR, filename)
|
| 44 |
+
write(filepath, sample_rate, scaled)
|
| 45 |
+
|
| 46 |
+
# Send the audio file to the external API
|
| 47 |
+
with open(filepath, 'rb') as f:
|
| 48 |
+
files = {'file': (filename, f)}
|
| 49 |
+
response = requests.post('https://detect-language-tuwk5nvu4a-uc.a.run.app', files=files)
|
| 50 |
+
|
| 51 |
+
if response.status_code == 200:
|
| 52 |
+
# Process successful response
|
| 53 |
+
return response.text
|
| 54 |
+
else:
|
| 55 |
+
# Handle error response
|
| 56 |
+
raise ValueError(f"API call failed with status code {response.status_code}: {response.text}")
|
| 57 |
+
|
| 58 |
+
# Create Gradio interface
|
| 59 |
+
with gr.Blocks() as demo:
|
| 60 |
+
gr.Markdown("#AI Language Detector")
|
| 61 |
+
gr.Markdown("## Transcribe audio with OpenAI's Whisper model and use GPT to detect the language from the transcription text")
|
| 62 |
+
with gr.Row():
|
| 63 |
+
record_btn = gr.Audio(sources=['microphone'], type='numpy', label='Record Audio')
|
| 64 |
+
transcribe_btn = gr.Button('Detect Language')
|
| 65 |
+
|
| 66 |
+
# Output message
|
| 67 |
+
output = gr.Textbox(label='Detected Language')
|
| 68 |
+
|
| 69 |
+
# Define button action
|
| 70 |
+
transcribe_btn.click(fn=save_and_transcribe_audio, inputs=[record_btn], outputs=[output])
|
| 71 |
+
|
| 72 |
+
# Launch the Gradio app
|
| 73 |
+
if __name__ == "__main__":
|
| 74 |
+
demo.launch(share=True, )
|