maliahson commited on
Commit
cbbec30
·
verified ·
1 Parent(s): e32471c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +150 -137
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
- # Login to HugChat
20
- sign = Login(EMAIL, PASSWD)
21
- cookies = sign.login(cookie_dir_path=cookie_path_dir, save_cookies=True)
22
- chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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] # Remove extension
74
  parts = base.split("_")
75
  if len(parts) >= 3:
76
  return {
77
  "agent_username": parts[0],
78
- "location": parts[-2] # Second-last chunk
79
  }
80
  return {"agent_username": "Unknown", "location": "Unknown"}
81
 
82
- # Extract metadata from file name
83
- metadata = extract_metadata(file_name)
84
- location = metadata["location"]
85
-
86
- # Step 1: Translate Urdu to English with context-aware correction
87
- english_text = chatbot.chat(
88
- 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}"
89
- ).wait_until_done()
90
-
91
- # Step 2: Extract specific crops and diseases from the English text
92
- extraction_prompt = f"""
93
- Below is an English text about specific crops and possible diseases/pests:
94
-
95
- {english_text}
96
-
97
- Identify each specific Crop (like wheat, rice, cotton, etc.) mentioned and list any Diseases or Pests affecting that crop.
98
-
99
- - If a disease or pest is mentioned without specifying a particular crop, list it under "No crop:".
100
- - If a crop is mentioned but no diseases or pests are specified for it, include it with an empty diseases list.
101
- - Do not include general terms like "crops" as a specific crop name.
102
-
103
- Format your answer in this style (one entry at a time):
104
-
105
- For specific crops with diseases:
106
- 1. CropName:
107
- Diseases:
108
- - DiseaseName
109
- - AnotherDisease
110
-
111
- For specific crops with no diseases:
112
- 2. NextCrop:
113
- Diseases:
114
-
115
- For standalone diseases:
116
- 3. No crop:
117
- Diseases:
118
- - StandaloneDisease
119
-
120
- No extra text, just the structured bullet list.
121
- """
122
- extraction_response = chatbot.chat(extraction_prompt).wait_until_done()
123
-
124
- # Step 3: Parse the extraction response
125
- lines = extraction_response.splitlines()
126
- crops_and_diseases = []
127
- current_crop = None
128
- current_diseases = []
129
-
130
- for line in lines:
131
- line = line.strip()
132
- if not line:
133
- continue
134
-
135
- # Match lines like "1. Wheat:" or "3. No crop:"
136
- match_crop = re.match(r'^(\d+)\.\s*(.+?):$', line)
137
- if match_crop:
138
- # Save previous crop and diseases if any
139
- if current_crop is not None or current_diseases:
140
- crops_and_diseases.append({
141
- "crop": current_crop,
142
- "diseases": current_diseases
143
- })
144
- # Process new crop
145
- crop_name = match_crop.group(2).strip()
146
- # Handle general terms as "No crop"
147
- if crop_name.lower() in ["no crop", "crops", "general crops"]:
148
- current_crop = None # Standalone diseases
149
- else:
150
- current_crop = crop_name
151
- current_diseases = []
152
- continue
153
-
154
- # Skip "Diseases:" line
155
- if line.lower().startswith("diseases:"):
156
- continue
157
-
158
- # Add disease if line starts with '-'
159
- if line.startswith('-'):
160
- disease_name = line.lstrip('-').strip()
161
- if disease_name:
162
- current_diseases.append(disease_name)
163
-
164
- # Append the last crop/diseases if present
165
- if current_crop is not None or current_diseases:
166
- crops_and_diseases.append({
167
- "crop": current_crop,
168
- "diseases": current_diseases
169
- })
170
-
171
- # Step 4: Get temperature for the location
172
- temp_prompt = f"Give me weather of {location} in Celsius numeric only."
173
- temperature_response = chatbot.chat(temp_prompt).wait_until_done()
174
-
175
- # Parse temperature (extract first number found)
176
- temperature = None
177
- temp_match = re.search(r'(\d+)', temperature_response)
178
- if temp_match:
179
- temperature = int(temp_match.group(1))
180
-
181
- # Step 5: Build and output the final JSON
182
- output = {
183
- "urdu_text": urdu_text,
184
- "english_text": english_text,
185
- "crops_and_diseases": crops_and_diseases,
186
- "temperature": temperature,
187
- "location": location
188
- }
189
-
190
- print(json.dumps(output))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()