Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,17 +3,27 @@ import requests
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
from transformers import pipeline
|
| 6 |
-
|
| 7 |
-
# Load a speech-to-text model
|
| 8 |
-
# Using a smaller model like 'distil-whisper/distil-small.en' for efficiency
|
| 9 |
-
# You might need to install 'pip install transformers torch soundfile librosa'
|
| 10 |
-
asr_pipeline = pipeline("automatic-speech-recognition", model="distil-whisper/distil-small.en")
|
| 11 |
|
| 12 |
# Hugging Face API details for Mistral
|
| 13 |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 14 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 15 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Prompt template
|
| 18 |
PROMPT_TEMPLATE = """
|
| 19 |
You are an AI translation assistant for a real-time universal translator.
|
|
@@ -65,7 +75,7 @@ def translate(text, source_lang, target_lang, emotion):
|
|
| 65 |
}
|
| 66 |
|
| 67 |
try:
|
| 68 |
-
raw_text = output[
|
| 69 |
parsed = json.loads(raw_text.strip())
|
| 70 |
except Exception as e:
|
| 71 |
parsed = {
|
|
@@ -81,6 +91,7 @@ def gradio_interface(audio, text, source_lang, target_lang, emotion):
|
|
| 81 |
if audio is not None:
|
| 82 |
try:
|
| 83 |
# Transcribe the audio file using the ASR pipeline
|
|
|
|
| 84 |
transcribed_text = asr_pipeline(audio)["text"]
|
| 85 |
# If there's also text input, combine them
|
| 86 |
if text:
|
|
|
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
from transformers import pipeline
|
| 6 |
+
from huggingface_hub import login
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Hugging Face API details for Mistral
|
| 9 |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2"
|
| 10 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Retrieve the token from environment variables
|
| 11 |
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 12 |
|
| 13 |
+
# Log in to Hugging Face Hub programmatically
|
| 14 |
+
if HF_TOKEN:
|
| 15 |
+
login(token=HF_TOKEN, add_to_git_credential=False)
|
| 16 |
+
else:
|
| 17 |
+
print("Warning: HF_TOKEN environment variable not set. Some models may not be accessible.")
|
| 18 |
+
|
| 19 |
+
# Load a speech-to-text model with authentication
|
| 20 |
+
# Pass the token explicitly to the pipeline function
|
| 21 |
+
asr_pipeline = pipeline(
|
| 22 |
+
"automatic-speech-recognition",
|
| 23 |
+
model="distil-whisper/distil-small.en",
|
| 24 |
+
use_auth_token=HF_TOKEN
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
# Prompt template
|
| 28 |
PROMPT_TEMPLATE = """
|
| 29 |
You are an AI translation assistant for a real-time universal translator.
|
|
|
|
| 75 |
}
|
| 76 |
|
| 77 |
try:
|
| 78 |
+
raw_text = output["generated_text"]
|
| 79 |
parsed = json.loads(raw_text.strip())
|
| 80 |
except Exception as e:
|
| 81 |
parsed = {
|
|
|
|
| 91 |
if audio is not None:
|
| 92 |
try:
|
| 93 |
# Transcribe the audio file using the ASR pipeline
|
| 94 |
+
# No token needed here as it's passed on initialization
|
| 95 |
transcribed_text = asr_pipeline(audio)["text"]
|
| 96 |
# If there's also text input, combine them
|
| 97 |
if text:
|