Soma Dhavala commited on
Commit
03d4021
Β·
1 Parent(s): ce6112f

Krishi GPT with ASR

Browse files
Files changed (5) hide show
  1. README.md +4 -4
  2. __pycache__/app.cpython-39.pyc +0 -0
  3. app.py +82 -0
  4. requirements.txt +3 -0
  5. temp.mp3 +0 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: KrishiGPT
3
- emoji: πŸ“š
4
- colorFrom: pink
5
- colorTo: red
6
  sdk: gradio
7
  sdk_version: 3.24.1
8
  app_file: app.py
 
1
  ---
2
+ title: AgGPT
3
+ emoji: πŸ’©
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 3.24.1
8
  app_file: app.py
__pycache__/app.cpython-39.pyc ADDED
Binary file (2.63 kB). View file
 
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ import gradio as gr
4
+ import openai
5
+ import os
6
+
7
+
8
+
9
+ api_key = os.getenv('OPEN_API_KEY')
10
+ openai.api_key = api_key
11
+
12
+
13
+ global_history = [{"role": "assistant", "content": "Hi, I am a chatbot. I can converse in English. I can answer your questions about farming in India. Ask me anything!"}]
14
+
15
+
16
+ from pydub import AudioSegment
17
+
18
+ def get_asr_output(audio_path,lang):
19
+ audio = AudioSegment.from_file(audio_path)
20
+ audio.export("temp.mp3", format="mp3")
21
+ file = open("temp.mp3","rb")
22
+ transcription = openai.Audio.transcribe("whisper-1", file, language=lang)
23
+ return transcription.text
24
+
25
+ def add_text(history, audio_path,lang):
26
+ global global_history
27
+ text = get_asr_output(audio_path,lang_dict[lang])
28
+ history = history + [(text, None)]
29
+ global_history = global_history+[{"role": "user", "content": text}]
30
+ print(global_history)
31
+ return history, ""
32
+
33
+ def get_chatgpt_response(history):
34
+ output = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=history)
35
+ history.append({"role": "assistant", "content": output.choices[0].message.content})
36
+ return output.choices[0].message.content, history
37
+
38
+ def bot(history):
39
+ global global_history
40
+ response, global_history = get_chatgpt_response(global_history)
41
+ history[-1][1] = response
42
+ return history
43
+
44
+ def clear_history(lang = "English"):
45
+ global global_history
46
+ global_history = [{"role": "assistant", "content": "Hi, I am a chatbot. I can converse in {}. I can answer your questions about farming in India. Ask me anything!".format(lang)}]
47
+ return None
48
+
49
+ lang_dict = {
50
+ "English": "en",
51
+ "Hindi": "hi",
52
+ "Bengali": "bn",
53
+ "Gujarati": "gu",
54
+ "Kannada": "kn",
55
+ "Marathi": "mr",
56
+ "Tamil": "ta",
57
+ "Telugu": "te"
58
+ }
59
+
60
+ with gr.Blocks(title="Krishi GPT Demo") as demo:
61
+ lang = gr.Radio(list(lang_dict.keys()), label="Select a Language")
62
+
63
+
64
+
65
+ with gr.Row():
66
+ with gr.Column():
67
+ user_audio = gr.Audio(source="microphone",type="filepath",label = "Speak your query")
68
+ txt = gr.Textbox(
69
+ show_label=True,
70
+ placeholder="Enter text and press enter, or Record your Audio",
71
+ visible=False,
72
+ ).style(container=False)
73
+
74
+ submit = gr.Button("Submit")
75
+ chatbot = gr.Chatbot([], elem_id="chatbot").style(height=500)
76
+ clear = gr.Button("Clear")
77
+ submit.click(add_text, [chatbot, user_audio,lang], [chatbot, txt]).then(bot, chatbot, chatbot)
78
+ clear.click(clear_history, [lang], chatbot, queue=False)
79
+ lang.change(clear_history, [lang], chatbot, queue=False)
80
+
81
+
82
+ demo.launch(share=False)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ openai==0.27.4
2
+ gradio==3.21.0
3
+ pydub==0.25.1
temp.mp3 ADDED
Binary file (18.7 kB). View file