Shahzad8515 commited on
Commit
ccba0b6
·
verified ·
1 Parent(s): d3d61da

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -8
app.py CHANGED
@@ -25,7 +25,7 @@ model = AutoModel.from_pretrained("bert-base-uncased")
25
  dimension = 768 # Size of BERT embeddings
26
  index = faiss.IndexFlatL2(dimension)
27
 
28
- # Folder path containing agriculture-related PDFs
29
  pdf_folder_path = "agriculture_pdfs"
30
 
31
  # Function to convert audio file to text
@@ -138,13 +138,12 @@ def process_audio(audio_file):
138
 
139
  if not combined_text.strip():
140
  return "No relevant information found in the PDFs", None
141
-
142
- # Modify the prompt to reflect the agricultural domain
143
  prompt = (
144
- f"The user has provided the following agriculture-related query: {combined_text}. "
145
  "Based on this, provide 4 clear and actionable steps to address the agricultural issue. "
146
  "The response should be concise and no longer than 5 lines."
147
- )
 
148
 
149
  print(f"Prompt: {prompt}") # Debugging line
150
 
@@ -158,11 +157,37 @@ def process_audio(audio_file):
158
  model="llama-3.1-70b-versatile",
159
  )
160
  response = chat_completion.choices[0].message.content
161
- output_file = "agriculture_advice.mp3"
162
  output_path = text_to_speech(response, output_file)
163
 
164
  if output_path is None:
165
  return "Error generating speech output", None
166
 
167
- return response
168
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  dimension = 768 # Size of BERT embeddings
26
  index = faiss.IndexFlatL2(dimension)
27
 
28
+ # Folder path containing PDFs
29
  pdf_folder_path = "agriculture_pdfs"
30
 
31
  # Function to convert audio file to text
 
138
 
139
  if not combined_text.strip():
140
  return "No relevant information found in the PDFs", None
 
 
141
  prompt = (
142
+ f"The user has provided the following agriculture-related query: {combined_text}. "
143
  "Based on this, provide 4 clear and actionable steps to address the agricultural issue. "
144
  "The response should be concise and no longer than 5 lines."
145
+ )
146
+
147
 
148
  print(f"Prompt: {prompt}") # Debugging line
149
 
 
157
  model="llama-3.1-70b-versatile",
158
  )
159
  response = chat_completion.choices[0].message.content
160
+ output_file = "advice.mp3"
161
  output_path = text_to_speech(response, output_file)
162
 
163
  if output_path is None:
164
  return "Error generating speech output", None
165
 
166
+ return response, output_path
167
+ except Exception as e:
168
+ print(f"Error in process_audio: {e}")
169
+ return "An error occurred while processing the audio", None
170
+
171
+ iface = gr.Interface(
172
+ fn=process_audio,
173
+ inputs=gr.Audio(type="filepath"),
174
+ outputs=[gr.Textbox(label="Advice"), gr.Audio(label="Advice Audio")],
175
+ title="Kisaan Dost",
176
+ description="Helping Farmers about their Crops",
177
+ theme="default",
178
+ article=(
179
+ "<div style='text-align: center; color: #001f43;'>"
180
+ "<h1>Chill Mama</h1>"
181
+ "<h3 style='font-size: 24px;'>Helping Parents to Nurture Healthy Families</h3>"
182
+ "</div>"
183
+ ),
184
+ css=(
185
+ "<style>"
186
+ "body { background-color: #BAE5F4; color: #80826; }"
187
+ "h1, h3 { color: #001f43; }"
188
+ "</style>"
189
+ )
190
+ )
191
+
192
+ if __name__ == "__main__":
193
+ iface.launch()