Update app.py
Browse files
app.py
CHANGED
@@ -7,19 +7,41 @@ import re
|
|
7 |
import torch
|
8 |
from transformers import pipeline
|
9 |
import librosa
|
|
|
|
|
10 |
|
11 |
# HugChat login credentials from environment variables (secrets)
|
12 |
EMAIL = os.environ.get("Email")
|
13 |
PASSWD = os.environ.get("Password")
|
14 |
|
|
|
|
|
|
|
|
|
15 |
# Directory to store cookies
|
16 |
cookie_path_dir = "./cookies/"
|
17 |
os.makedirs(cookie_path_dir, exist_ok=True)
|
18 |
|
19 |
-
#
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Model and device configuration for Whisper transcription
|
25 |
MODEL_NAME = "openai/whisper-large-v3-turbo"
|
@@ -38,153 +60,144 @@ def transcribe_audio(audio_path):
|
|
38 |
Transcribe a local audio file using the Whisper pipeline.
|
39 |
"""
|
40 |
try:
|
41 |
-
# Ensure audio is mono and resampled to 16kHz
|
42 |
audio, sr = librosa.load(audio_path, sr=16000, mono=True)
|
43 |
-
|
44 |
-
# Perform transcription
|
45 |
transcription = pipe(audio, batch_size=8, generate_kwargs={"language": "urdu"})["text"]
|
46 |
return transcription
|
47 |
-
|
48 |
except Exception as e:
|
49 |
return f"Error processing audio: {e}"
|
50 |
|
51 |
-
# Get command-line arguments: audio file path and file name
|
52 |
-
audio_path = sys.argv[1] # Path to the audio file
|
53 |
-
file_name = sys.argv[2] # File name for metadata
|
54 |
-
|
55 |
-
# Transcribe the audio to get Urdu text
|
56 |
-
urdu_text = transcribe_audio(audio_path)
|
57 |
-
if "Error" in urdu_text:
|
58 |
-
print(json.dumps({"error": urdu_text}))
|
59 |
-
sys.exit(1)
|
60 |
-
|
61 |
def extract_metadata(file_name):
|
62 |
"""
|
63 |
Extract metadata from the file name.
|
64 |
-
Assumes the second-last chunk is the city, e.g.,
|
65 |
-
'agent2_5_Multan_Pakistan.mp3' -> location = 'Multan'.
|
66 |
-
|
67 |
-
Args:
|
68 |
-
file_name (str): The name of the audio file.
|
69 |
-
|
70 |
-
Returns:
|
71 |
-
dict: Contains agent_username and location.
|
72 |
"""
|
73 |
-
base = file_name.split(".")[0]
|
74 |
parts = base.split("_")
|
75 |
if len(parts) >= 3:
|
76 |
return {
|
77 |
"agent_username": parts[0],
|
78 |
-
"location": parts[-2]
|
79 |
}
|
80 |
return {"agent_username": "Unknown", "location": "Unknown"}
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
if
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
if
|
160 |
-
|
161 |
-
|
162 |
-
current_diseases
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
"
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
import torch
|
8 |
from transformers import pipeline
|
9 |
import librosa
|
10 |
+
import gradio as gr
|
11 |
+
import requests # Added for debugging network
|
12 |
|
13 |
# HugChat login credentials from environment variables (secrets)
|
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 |
+
# Test network connectivity to Hugging Face
|
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 |
+
sign = Login(EMAIL, PASSWD)
|
35 |
+
print("Attempting login with hugchat...")
|
36 |
+
cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
|
37 |
+
print("Login successful, cookies obtained.")
|
38 |
+
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
|
39 |
+
except Exception as e:
|
40 |
+
print(f"Login failed with error: {str(e)}")
|
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"
|
|
|
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"]
|
65 |
return transcription
|
|
|
66 |
except Exception as e:
|
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:
|
76 |
return {
|
77 |
"agent_username": parts[0],
|
78 |
+
"location": parts[-2]
|
79 |
}
|
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})
|
89 |
+
|
90 |
+
metadata = extract_metadata(file_name)
|
91 |
+
location = metadata["location"]
|
92 |
+
|
93 |
+
english_text = chatbot.chat(
|
94 |
+
f"The following Urdu text is about crops and their diseases, but it may contain errors or misheard words due to audio transcription issues. Please use context to infer the most likely correct crop names and disease terms, and then translate the text to English:\n\n{urdu_text}"
|
95 |
+
).wait_until_done()
|
96 |
+
|
97 |
+
extraction_prompt = f"""
|
98 |
+
Below is an English text about specific crops and possible diseases/pests:
|
99 |
+
|
100 |
+
{english_text}
|
101 |
+
|
102 |
+
Identify each specific Crop (like wheat, rice, cotton, etc.) mentioned and list any Diseases or Pests affecting that crop.
|
103 |
+
|
104 |
+
- If a disease or pest is mentioned without specifying a particular crop, list it under "No crop:".
|
105 |
+
- If a crop is mentioned but no diseases or pests are specified for it, include it with an empty diseases list.
|
106 |
+
- Do not include general terms like "crops" as a specific crop name.
|
107 |
+
|
108 |
+
Format your answer in this style (one entry at a time):
|
109 |
+
|
110 |
+
For specific crops with diseases:
|
111 |
+
1. CropName:
|
112 |
+
Diseases:
|
113 |
+
- DiseaseName
|
114 |
+
- AnotherDisease
|
115 |
+
|
116 |
+
For specific crops with no diseases:
|
117 |
+
2. NextCrop:
|
118 |
+
Diseases:
|
119 |
+
|
120 |
+
For standalone diseases:
|
121 |
+
3. No crop:
|
122 |
+
Diseases:
|
123 |
+
- StandaloneDisease
|
124 |
+
|
125 |
+
No extra text, just the structured bullet list.
|
126 |
+
"""
|
127 |
+
extraction_response = chatbot.chat(extraction_prompt).wait_until_done()
|
128 |
+
|
129 |
+
lines = extraction_response.splitlines()
|
130 |
+
crops_and_diseases = []
|
131 |
+
current_crop = None
|
132 |
+
current_diseases = []
|
133 |
+
|
134 |
+
for line in lines:
|
135 |
+
line = line.strip()
|
136 |
+
if not line:
|
137 |
+
continue
|
138 |
+
match_crop = re.match(r'^(\d+)\.\s*(.+?):$', line)
|
139 |
+
if match_crop:
|
140 |
+
if current_crop is not None or current_diseases:
|
141 |
+
crops_and_diseases.append({
|
142 |
+
"crop": current_crop,
|
143 |
+
"diseases": current_diseases
|
144 |
+
})
|
145 |
+
crop_name = match_crop.group(2).strip()
|
146 |
+
if crop_name.lower() in ["no crop", "crops", "general crops"]:
|
147 |
+
current_crop = None
|
148 |
+
else:
|
149 |
+
current_crop = crop_name
|
150 |
+
current_diseases = []
|
151 |
+
continue
|
152 |
+
if line.lower().startswith("diseases:"):
|
153 |
+
continue
|
154 |
+
if line.startswith('-'):
|
155 |
+
disease_name = line.lstrip('-').strip()
|
156 |
+
if disease_name:
|
157 |
+
current_diseases.append(disease_name)
|
158 |
+
|
159 |
+
if current_crop is not None or current_diseases:
|
160 |
+
crops_and_diseases.append({
|
161 |
+
"crop": current_crop,
|
162 |
+
"diseases": current_diseases
|
163 |
+
})
|
164 |
+
|
165 |
+
temp_prompt = f"Give me weather of {location} in Celsius numeric only."
|
166 |
+
temperature_response = chatbot.chat(temp_prompt).wait_until_done()
|
167 |
+
|
168 |
+
temperature = None
|
169 |
+
temp_match = re.search(r'(\d+)', temperature_response)
|
170 |
+
if temp_match:
|
171 |
+
temperature = int(temp_match.group(1))
|
172 |
+
|
173 |
+
output = {
|
174 |
+
"urdu_text": urdu_text,
|
175 |
+
"english_text": english_text,
|
176 |
+
"crops_and_diseases": crops_and_diseases,
|
177 |
+
"temperature": temperature,
|
178 |
+
"location": location
|
179 |
+
}
|
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 |
+
|
187 |
+
with gr.Row():
|
188 |
+
audio_input = gr.Audio(type="filepath", label="Upload Audio File (Urdu)")
|
189 |
+
file_name_input = gr.Textbox(label="File Name for Metadata (e.g., agent2_5_Multan_Pakistan.mp3)", placeholder="Enter file name")
|
190 |
+
|
191 |
+
with gr.Row():
|
192 |
+
output_json = gr.JSON(label="Output (Urdu, English, Crops with Diseases)")
|
193 |
+
|
194 |
+
process_button = gr.Button("Process Audio")
|
195 |
+
|
196 |
+
process_button.click(
|
197 |
+
fn=process_audio,
|
198 |
+
inputs=[audio_input, file_name_input],
|
199 |
+
outputs=[output_json],
|
200 |
+
)
|
201 |
+
|
202 |
+
if __name__ == "__main__":
|
203 |
+
interface.launch()
|