TanU21 commited on
Commit
ae5a762
·
verified ·
1 Parent(s): b59016b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -86
app.py CHANGED
@@ -1,87 +1,88 @@
1
- import datetime
2
- import threading
3
- import time
4
- from langchain_groq import ChatGroq
5
- from dotenv import load_dotenv
6
- import gradio as gr
7
- import os
8
- from gtts import gTTS
9
- import io
10
- import pygame
11
-
12
- load_dotenv()
13
- groq_api_key = os.environ.get("GROQ_API_KEY")
14
- llm = ChatGroq(
15
- model="mixtral-8x7b-32768",
16
- temperature=0.7,
17
- api_key=groq_api_key,
18
- verbose = False
19
- )
20
-
21
- prompt = ChatPromptTemplate.from_messages(
22
- [
23
- ("system", "You are an intelligent AI assistant. Answer the questions as accurately as possible."),
24
- ("human", "{input}")
25
- ]
26
- )
27
-
28
- chain = prompt | llm
29
-
30
- convo_history = []
31
- audio_playing = False
32
-
33
- def stop_audio():
34
- global audio_playing
35
- if audio_playing:
36
- pygame.mixer.music.stop()
37
- audio_playing = False
38
-
39
- def play_tts(text):
40
- global audio_playing
41
- audio_playing = True
42
-
43
- def speak_async():
44
- global audio_playing
45
- pygame.mixer.init()
46
- fp = io.BytesIO()
47
- gTTS(text).write_to_fp(fp)
48
- fp.seek(0)
49
- pygame.mixer.music.load(fp)
50
- pygame.mixer.music.play()
51
- while pygame.mixer.music.get_busy():
52
- pygame.time.Clock().tick(10)
53
- audio_playing = False
54
- pygame.mixer.quit()
55
-
56
- thread = threading.Thread(target=speak_async)
57
- thread.start()
58
-
59
- def ChatBot(text):
60
-
61
- global convo_history, audio_playing
62
-
63
- stop_audio()
64
-
65
- convo_history.append(f"Human: {text}")
66
-
67
- full_context = "\n".join(convo_history)
68
- response = chain.invoke({"input": full_context}).content
69
-
70
- convo_history.append(f"AI: {response}")
71
-
72
- print("\n".join(convo_history))
73
-
74
- play_tts(response)
75
-
76
- return "\n".join(convo_history)
77
-
78
-
79
- demo = gr.Interface(
80
- fn=ChatBot,
81
- inputs="text",
82
- outputs="text",
83
- title="AudioBOT",
84
- description="Ask any question to the AI. It remembers the context of the conversation and speaks its response.",
85
- )
86
-
 
87
  demo.launch()
 
1
+ import datetime
2
+ import threading
3
+ import time
4
+ from langchain_core.prompts import ChatPromptTemplate
5
+ from langchain_groq import ChatGroq
6
+ from dotenv import load_dotenv
7
+ import gradio as gr
8
+ import os
9
+ from gtts import gTTS
10
+ import io
11
+ import pygame
12
+
13
+ load_dotenv()
14
+ groq_api_key = os.environ.get("GROQ_API_KEY")
15
+ llm = ChatGroq(
16
+ model="mixtral-8x7b-32768",
17
+ temperature=0.7,
18
+ api_key=groq_api_key,
19
+ verbose = False
20
+ )
21
+
22
+ prompt = ChatPromptTemplate.from_messages(
23
+ [
24
+ ("system", "You are an intelligent AI assistant. Answer the questions as accurately as possible."),
25
+ ("human", "{input}")
26
+ ]
27
+ )
28
+
29
+ chain = prompt | llm
30
+
31
+ convo_history = []
32
+ audio_playing = False
33
+
34
+ def stop_audio():
35
+ global audio_playing
36
+ if audio_playing:
37
+ pygame.mixer.music.stop()
38
+ audio_playing = False
39
+
40
+ def play_tts(text):
41
+ global audio_playing
42
+ audio_playing = True
43
+
44
+ def speak_async():
45
+ global audio_playing
46
+ pygame.mixer.init()
47
+ fp = io.BytesIO()
48
+ gTTS(text).write_to_fp(fp)
49
+ fp.seek(0)
50
+ pygame.mixer.music.load(fp)
51
+ pygame.mixer.music.play()
52
+ while pygame.mixer.music.get_busy():
53
+ pygame.time.Clock().tick(10)
54
+ audio_playing = False
55
+ pygame.mixer.quit()
56
+
57
+ thread = threading.Thread(target=speak_async)
58
+ thread.start()
59
+
60
+ def ChatBot(text):
61
+
62
+ global convo_history, audio_playing
63
+
64
+ stop_audio()
65
+
66
+ convo_history.append(f"Human: {text}")
67
+
68
+ full_context = "\n".join(convo_history)
69
+ response = chain.invoke({"input": full_context}).content
70
+
71
+ convo_history.append(f"AI: {response}")
72
+
73
+ print("\n".join(convo_history))
74
+
75
+ play_tts(response)
76
+
77
+ return "\n".join(convo_history)
78
+
79
+
80
+ demo = gr.Interface(
81
+ fn=ChatBot,
82
+ inputs="text",
83
+ outputs="text",
84
+ title="AudioBOT",
85
+ description="Ask any question to the AI. It remembers the context of the conversation and speaks its response.",
86
+ )
87
+
88
  demo.launch()