navjotk commited on
Commit
ab80966
·
verified ·
1 Parent(s): 089b37c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -2
app.py CHANGED
@@ -2,11 +2,13 @@ import gradio as gr
2
  import pandas as pd
3
  import lightgbm as lgb
4
  import numpy as np
5
- import os
6
  from sklearn.model_selection import train_test_split
7
  from sklearn.preprocessing import LabelEncoder
8
  from transformers import pipeline, set_seed
9
  import google.generativeai as genai
 
 
 
10
 
11
  # ---------------------------
12
  # Load environment variables
@@ -62,6 +64,18 @@ def smart_chat(user_input, model_choice):
62
  else:
63
  return chat_with_bot(user_input)
64
 
 
 
 
 
 
 
 
 
 
 
 
 
65
  # ---------------------------
66
  # GRADIO APP UI
67
  # ---------------------------
@@ -111,7 +125,12 @@ with gr.Blocks() as demo:
111
  chatbot_output = gr.Textbox(label="AgroVision Bot Response")
112
  chatbot_btn = gr.Button("Ask")
113
 
114
- chatbot_btn.click(fn=smart_chat, inputs=[user_input, model_selector], outputs=chatbot_output)
 
 
 
 
 
115
 
116
  # Launch the app
117
  demo.launch()
 
2
  import pandas as pd
3
  import lightgbm as lgb
4
  import numpy as np
 
5
  from sklearn.model_selection import train_test_split
6
  from sklearn.preprocessing import LabelEncoder
7
  from transformers import pipeline, set_seed
8
  import google.generativeai as genai
9
+ import pyttsx3
10
+ import tempfile
11
+ import os
12
 
13
  # ---------------------------
14
  # Load environment variables
 
64
  else:
65
  return chat_with_bot(user_input)
66
 
67
+ # ---------------------------
68
+ # Text-to-Speech Function
69
+ # ---------------------------
70
+ def text_to_speech(text):
71
+ # Initialize the pyttsx3 engine
72
+ engine = pyttsx3.init()
73
+ # Save the audio to a temporary file
74
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
75
+ engine.save_to_file(text, temp_file.name)
76
+ engine.runAndWait()
77
+ return temp_file.name
78
+
79
  # ---------------------------
80
  # GRADIO APP UI
81
  # ---------------------------
 
125
  chatbot_output = gr.Textbox(label="AgroVision Bot Response")
126
  chatbot_btn = gr.Button("Ask")
127
 
128
+ def respond_with_audio_and_text(user_input, model_choice):
129
+ response_text = smart_chat(user_input, model_choice)
130
+ audio_file = text_to_speech(response_text)
131
+ return response_text, audio_file
132
+
133
+ chatbot_btn.click(fn=respond_with_audio_and_text, inputs=[user_input, model_selector], outputs=[chatbot_output, audio_input])
134
 
135
  # Launch the app
136
  demo.launch()