Shahzad8515 commited on
Commit
5ef27d0
·
verified ·
1 Parent(s): 6adabed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -43
app.py CHANGED
@@ -35,13 +35,10 @@ def audio_to_text(audio_file_path):
35
  with sr.AudioFile(audio_file_path) as source:
36
  audio = recognizer.record(source)
37
  text = recognizer.recognize_google(audio)
38
- print(f"Extracted Text: {text}") # Debugging line
39
  return text
40
  except sr.UnknownValueError:
41
- print("Audio could not be understood") # Debugging line
42
  return None
43
  except sr.RequestError:
44
- print("Request error") # Debugging line
45
  return None
46
 
47
  # Function to convert audio to WAV format
@@ -53,8 +50,7 @@ def convert_to_wav(audio_file_path):
53
  wav_path = "temp_audio.wav"
54
  audio.export(wav_path, format="wav")
55
  return wav_path
56
- except Exception as e:
57
- print(f"Error converting audio to WAV: {e}")
58
  return None
59
 
60
  # Function to extract text from a PDF file
@@ -65,8 +61,8 @@ def extract_text_from_pdf(pdf_file):
65
  for page_num in range(len(pdf_document)):
66
  page = pdf_document.load_page(page_num)
67
  text += page.get_text()
68
- except Exception as e:
69
- print(f"Error extracting text from PDF: {e}")
70
  return text
71
 
72
  # Function to embed text using a transformer model
@@ -76,8 +72,7 @@ def embed_text(texts, model, tokenizer):
76
  with torch.no_grad():
77
  embeddings = model(**inputs).last_hidden_state.mean(dim=1).numpy()
78
  return embeddings
79
- except Exception as e:
80
- print(f"Error embedding text: {e}")
81
  return np.array([]) # Return empty array on error
82
 
83
  # Function to convert text to speech
@@ -86,8 +81,7 @@ def text_to_speech(text, output_file):
86
  tts = gTTS(text=text, lang='en')
87
  tts.save(output_file)
88
  return output_file
89
- except Exception as e:
90
- print(f"Error converting text to speech: {e}")
91
  return None
92
 
93
  # Read all PDF files from the specified folder
@@ -98,25 +92,17 @@ for path in pdf_paths:
98
  pdf_text = extract_text_from_pdf(path)
99
  if pdf_text:
100
  texts.append(pdf_text)
101
- else:
102
- print(f"Failed to extract text from {path}")
103
 
104
  # Embed PDF texts and add to vector database
105
  embeddings = embed_text(texts, model, tokenizer)
106
  if embeddings.size > 0:
107
  index.add(embeddings)
108
- else:
109
- print("No embeddings to add to the vector database")
110
 
111
  def process_audio(audio_file):
112
  if audio_file is None:
113
- return "No audio file provided", None # Handle case where no file is uploaded
114
-
115
- if isinstance(audio_file, str):
116
- audio_file_path = audio_file
117
- else:
118
- audio_file_path = audio_file.name
119
 
 
120
  wav_path = convert_to_wav(audio_file_path)
121
  if wav_path is None:
122
  return "Error converting audio file to WAV format", None
@@ -138,15 +124,12 @@ def process_audio(audio_file):
138
 
139
  if not combined_text.strip():
140
  return "No relevant information found in the PDFs", None
 
141
  prompt = (
142
- f"The user has asked a query related to agricultural practices: {text}. "
143
- f"Here are relevant excerpts from the Better Crops South Asia document: {combined_text}. "
144
- "Based on this information, please provide accurate advice related to sustainable crop management, pest control, irrigation practices, and any recommendations for improving crop yield in the South Asian region."
145
- )
146
-
147
-
148
-
149
- print(f"Prompt: {prompt}") # Debugging line
150
 
151
  chat_completion = client.chat.completions.create(
152
  messages=[
@@ -165,30 +148,51 @@ def process_audio(audio_file):
165
  return "Error generating speech output", None
166
 
167
  return response, output_path
168
- except Exception as e:
169
- print(f"Error in process_audio: {e}")
170
  return "An error occurred while processing the audio", None
171
 
 
172
  iface = gr.Interface(
173
  fn=process_audio,
174
  inputs=gr.Audio(type="filepath"),
175
- outputs=[gr.Textbox(label="Advice"), gr.Audio(label="Advice Audio")],
176
- title="BetterCrops",
177
- description="Helping Farmers with their Crops",
178
- theme="default",
 
 
179
  article=(
180
- "<div style='text-align: center; color: #001f43;'>"
181
- "<h1>BetterCrops</h1>"
182
- "<h3 style='font-size: 24px;'>Helping Farmers with their Crops</h3>"
183
  "</div>"
184
  ),
 
185
  css=(
186
- "<style>"
187
- "body { background-color: #BAE5F4; color: #80826; }"
188
- "h1, h3 { color: #001f43; }"
189
- "</style>"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
190
  )
191
  )
192
 
193
  if __name__ == "__main__":
194
- iface.launch()
 
35
  with sr.AudioFile(audio_file_path) as source:
36
  audio = recognizer.record(source)
37
  text = recognizer.recognize_google(audio)
 
38
  return text
39
  except sr.UnknownValueError:
 
40
  return None
41
  except sr.RequestError:
 
42
  return None
43
 
44
  # Function to convert audio to WAV format
 
50
  wav_path = "temp_audio.wav"
51
  audio.export(wav_path, format="wav")
52
  return wav_path
53
+ except Exception:
 
54
  return None
55
 
56
  # Function to extract text from a PDF file
 
61
  for page_num in range(len(pdf_document)):
62
  page = pdf_document.load_page(page_num)
63
  text += page.get_text()
64
+ except Exception:
65
+ pass
66
  return text
67
 
68
  # Function to embed text using a transformer model
 
72
  with torch.no_grad():
73
  embeddings = model(**inputs).last_hidden_state.mean(dim=1).numpy()
74
  return embeddings
75
+ except Exception:
 
76
  return np.array([]) # Return empty array on error
77
 
78
  # Function to convert text to speech
 
81
  tts = gTTS(text=text, lang='en')
82
  tts.save(output_file)
83
  return output_file
84
+ except Exception:
 
85
  return None
86
 
87
  # Read all PDF files from the specified folder
 
92
  pdf_text = extract_text_from_pdf(path)
93
  if pdf_text:
94
  texts.append(pdf_text)
 
 
95
 
96
  # Embed PDF texts and add to vector database
97
  embeddings = embed_text(texts, model, tokenizer)
98
  if embeddings.size > 0:
99
  index.add(embeddings)
 
 
100
 
101
  def process_audio(audio_file):
102
  if audio_file is None:
103
+ return "No audio file provided", None
 
 
 
 
 
104
 
105
+ audio_file_path = audio_file if isinstance(audio_file, str) else audio_file.name
106
  wav_path = convert_to_wav(audio_file_path)
107
  if wav_path is None:
108
  return "Error converting audio file to WAV format", None
 
124
 
125
  if not combined_text.strip():
126
  return "No relevant information found in the PDFs", None
127
+
128
  prompt = (
129
+ f"The user has asked a query related to agricultural practices: {text}. "
130
+ f"Here are relevant excerpts from the Better Crops South Asia document: {combined_text}. "
131
+ "Based on this information, please provide accurate advice related to sustainable crop management, pest control, irrigation practices, and any recommendations for improving crop yield in the South Asian region."
132
+ )
 
 
 
 
133
 
134
  chat_completion = client.chat.completions.create(
135
  messages=[
 
148
  return "Error generating speech output", None
149
 
150
  return response, output_path
151
+ except Exception:
 
152
  return "An error occurred while processing the audio", None
153
 
154
+ # Enhanced Gradio interface customization
155
  iface = gr.Interface(
156
  fn=process_audio,
157
  inputs=gr.Audio(type="filepath"),
158
+ outputs=[gr.Textbox(label="Advice", lines=10), gr.Audio(label="Advice Audio")],
159
+ title="🌾 BetterCrops: Agriculture Support for Farmers",
160
+ description=(
161
+ "💡 **BetterCrops** is designed to assist farmers with their crops by analyzing agricultural PDFs "
162
+ "and generating personalized audio advice based on your voice queries."
163
+ ),
164
  article=(
165
+ "<div style='text-align: center; color: #003f6e;'>"
166
+ "<h1 style='font-size: 36px; font-weight: bold;'>BetterCrops</h1>"
167
+ "<h3 style='font-size: 24px; font-weight: normal;'>Empowering Farmers with AI-driven Insights</h3>"
168
  "</div>"
169
  ),
170
+ theme="grass",
171
  css=(
172
+ """
173
+ body {
174
+ background-color: #f0f5e9;
175
+ color: #2f4f2f;
176
+ font-family: 'Helvetica Neue', sans-serif;
177
+ }
178
+ h1, h3 {
179
+ color: #003f6e;
180
+ }
181
+ .gradio-container {
182
+ padding: 20px;
183
+ background: linear-gradient(135deg, #a3cfba 0%, #e8f5e9 100%);
184
+ border-radius: 15px;
185
+ }
186
+ .gradio-inputs, .gradio-outputs {
187
+ margin: 20px;
188
+ padding: 20px;
189
+ background-color: #ffffff;
190
+ border-radius: 10px;
191
+ box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
192
+ }
193
+ """
194
  )
195
  )
196
 
197
  if __name__ == "__main__":
198
+ iface.launch()