Update app.py
Browse files
app.py
CHANGED
@@ -8,46 +8,25 @@ import torch
|
|
8 |
from transformers import pipeline
|
9 |
import librosa
|
10 |
import gradio as gr
|
11 |
-
import requests
|
12 |
|
13 |
-
#
|
14 |
-
EMAIL = os.environ.get("Email")
|
15 |
-
PASSWD = os.environ.get("Password")
|
16 |
-
|
17 |
-
# Debug: Print credentials to verify they're being read
|
18 |
-
print("EMAIL from env:", EMAIL)
|
19 |
-
print("PASSWORD from env:", PASSWD)
|
20 |
-
|
21 |
-
# Directory to store cookies
|
22 |
cookie_path_dir = "./cookies/"
|
23 |
os.makedirs(cookie_path_dir, exist_ok=True)
|
24 |
|
25 |
-
#
|
26 |
-
try:
|
27 |
-
response = requests.get("https://huggingface.co/login", timeout=10)
|
28 |
-
print("Network test: Successfully reached https://huggingface.co/login, status code:", response.status_code)
|
29 |
-
except Exception as e:
|
30 |
-
print("Network test failed:", str(e))
|
31 |
-
|
32 |
-
# Login to HugChat with detailed error handling
|
33 |
try:
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
print("Login successful, cookies obtained.")
|
38 |
-
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
39 |
except Exception as e:
|
40 |
-
print(f"
|
41 |
-
print("Full traceback:")
|
42 |
-
import traceback
|
43 |
-
traceback.print_exc()
|
44 |
sys.exit(1)
|
45 |
|
46 |
# Model and device configuration for Whisper transcription
|
47 |
MODEL_NAME = "openai/whisper-large-v3-turbo"
|
48 |
device = 0 if torch.cuda.is_available() else "cpu"
|
49 |
|
50 |
-
# Initialize Whisper pipeline
|
51 |
pipe = pipeline(
|
52 |
task="automatic-speech-recognition",
|
53 |
model=MODEL_NAME,
|
@@ -56,9 +35,6 @@ pipe = pipeline(
|
|
56 |
)
|
57 |
|
58 |
def transcribe_audio(audio_path):
|
59 |
-
"""
|
60 |
-
Transcribe a local audio file using the Whisper pipeline.
|
61 |
-
"""
|
62 |
try:
|
63 |
audio, sr = librosa.load(audio_path, sr=16000, mono=True)
|
64 |
transcription = pipe(audio, batch_size=8, generate_kwargs={"language": "urdu"})["text"]
|
@@ -67,9 +43,6 @@ def transcribe_audio(audio_path):
|
|
67 |
return f"Error processing audio: {e}"
|
68 |
|
69 |
def extract_metadata(file_name):
|
70 |
-
"""
|
71 |
-
Extract metadata from the file name.
|
72 |
-
"""
|
73 |
base = file_name.split(".")[0]
|
74 |
parts = base.split("_")
|
75 |
if len(parts) >= 3:
|
@@ -80,9 +53,6 @@ def extract_metadata(file_name):
|
|
80 |
return {"agent_username": "Unknown", "location": "Unknown"}
|
81 |
|
82 |
def process_audio(audio, file_name):
|
83 |
-
"""
|
84 |
-
Process the audio file and return Urdu transcription, English translation, and crops with diseases.
|
85 |
-
"""
|
86 |
urdu_text = transcribe_audio(audio)
|
87 |
if "Error" in urdu_text:
|
88 |
return json.dumps({"error": urdu_text})
|
@@ -180,7 +150,6 @@ def process_audio(audio, file_name):
|
|
180 |
|
181 |
return json.dumps(output)
|
182 |
|
183 |
-
# Gradio Interface
|
184 |
with gr.Blocks(title="Audio to Crop Disease API") as interface:
|
185 |
gr.Markdown("## Upload Audio to Get Urdu Transcription, English Translation, and Crop Diseases")
|
186 |
|
|
|
8 |
from transformers import pipeline
|
9 |
import librosa
|
10 |
import gradio as gr
|
11 |
+
import requests
|
12 |
|
13 |
+
# Directory to store/load cookies
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
cookie_path_dir = "./cookies/"
|
15 |
os.makedirs(cookie_path_dir, exist_ok=True)
|
16 |
|
17 |
+
# Load pre-saved cookies instead of logging in
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
try:
|
19 |
+
print("Attempting to load cookies from:", cookie_path_dir)
|
20 |
+
chatbot = hugchat.ChatBot(cookie_path_dir=cookie_path_dir)
|
21 |
+
print("Cookies loaded successfully.")
|
|
|
|
|
22 |
except Exception as e:
|
23 |
+
print(f"Failed to load cookies: {str(e)}")
|
|
|
|
|
|
|
24 |
sys.exit(1)
|
25 |
|
26 |
# Model and device configuration for Whisper transcription
|
27 |
MODEL_NAME = "openai/whisper-large-v3-turbo"
|
28 |
device = 0 if torch.cuda.is_available() else "cpu"
|
29 |
|
|
|
30 |
pipe = pipeline(
|
31 |
task="automatic-speech-recognition",
|
32 |
model=MODEL_NAME,
|
|
|
35 |
)
|
36 |
|
37 |
def transcribe_audio(audio_path):
|
|
|
|
|
|
|
38 |
try:
|
39 |
audio, sr = librosa.load(audio_path, sr=16000, mono=True)
|
40 |
transcription = pipe(audio, batch_size=8, generate_kwargs={"language": "urdu"})["text"]
|
|
|
43 |
return f"Error processing audio: {e}"
|
44 |
|
45 |
def extract_metadata(file_name):
|
|
|
|
|
|
|
46 |
base = file_name.split(".")[0]
|
47 |
parts = base.split("_")
|
48 |
if len(parts) >= 3:
|
|
|
53 |
return {"agent_username": "Unknown", "location": "Unknown"}
|
54 |
|
55 |
def process_audio(audio, file_name):
|
|
|
|
|
|
|
56 |
urdu_text = transcribe_audio(audio)
|
57 |
if "Error" in urdu_text:
|
58 |
return json.dumps({"error": urdu_text})
|
|
|
150 |
|
151 |
return json.dumps(output)
|
152 |
|
|
|
153 |
with gr.Blocks(title="Audio to Crop Disease API") as interface:
|
154 |
gr.Markdown("## Upload Audio to Get Urdu Transcription, English Translation, and Crop Diseases")
|
155 |
|