Dharun72 commited on
Commit
5e4a333
·
verified ·
1 Parent(s): 0502db7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -59
app.py CHANGED
@@ -1,60 +1,80 @@
1
- import os
2
- from langchain_groq import ChatGroq
3
- from langchain.chains import ConversationChain
4
- from langchain.memory import ConversationBufferMemory
5
- from dotenv import load_dotenv
6
- import gradio as gr
7
-
8
- # Load environment variables
9
- load_dotenv()
10
- groq_api_key = os.getenv('GROQ_API_KEY')
11
-
12
- if not groq_api_key:
13
- raise ValueError("GROQ_API_KEY not found in environment variables. Please add it to your .env file.")
14
-
15
- # Initialize chatbot components
16
- llm = ChatGroq(groq_api_key=groq_api_key, model_name="mixtral-8x7b-32768")
17
- memory = ConversationBufferMemory()
18
- conversation = ConversationChain(llm=llm, memory=memory)
19
-
20
- def add_message(history, message):
21
- history.append((message, None))
22
- return history, gr.Textbox(value="", interactive=True)
23
-
24
- def bot(history):
25
- if not history:
26
- initial_message = "Hello! How can I assist you today?"
27
- history.append((None, initial_message))
28
- return history
29
-
30
- message = history[-1][0]
31
-
32
- try:
33
- response = conversation.predict(input=message)
34
- except Exception as e:
35
- response = f"I'm sorry, but I encountered an error: {str(e)}"
36
-
37
- history[-1] = (history[-1][0], response)
38
- return history
39
-
40
- def clear_chat():
41
- global memory
42
- memory.clear()
43
- return []
44
-
45
- with gr.Blocks() as demo:
46
- with gr.Column():
47
- chatbot = gr.Chatbot([], elem_id="chatbot")
48
- msg = gr.Textbox(label="Message")
49
- clear = gr.Button("Clear")
50
-
51
- chat_msg = msg.submit(add_message, [chatbot, msg], [chatbot, msg], queue=False).then(
52
- bot, chatbot, chatbot
53
- )
54
-
55
- clear.click(clear_chat, None, chatbot, queue=False)
56
-
57
- demo.queue()
58
-
59
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  demo.launch()
 
1
+ import os
2
+ import time
3
+ from langchain_groq import ChatGroq
4
+ from langchain.chains import ConversationChain
5
+ from langchain.memory import ConversationBufferMemory
6
+ from dotenv import load_dotenv
7
+ import gradio as gr
8
+
9
+ # Load environment variables
10
+ load_dotenv()
11
+ groq_api_key = os.getenv('GROQ_API_KEY')
12
+
13
+ if not groq_api_key:
14
+ raise ValueError("GROQ_API_KEY not found in environment variables. Please add it to your .env file.")
15
+
16
+ # Global variables
17
+ stop_generation = False
18
+
19
+ # Initialize chatbot components
20
+ llm = ChatGroq(groq_api_key=groq_api_key, model_name="mixtral-8x7b-32768")
21
+ memory = ConversationBufferMemory()
22
+ conversation = ConversationChain(llm=llm, memory=memory)
23
+
24
+ def add_message(history, message):
25
+ history.append((message, None))
26
+ return history, gr.Textbox(value="", interactive=True)
27
+
28
+ def bot(history):
29
+ global stop_generation
30
+ stop_generation = False
31
+
32
+ if not history:
33
+ initial_message = "Hello! How can I assist you today?"
34
+ history.append((None, initial_message))
35
+ yield history
36
+ return
37
+
38
+ message = history[-1][0]
39
+
40
+ try:
41
+ response = conversation.predict(input=message)
42
+ except Exception as e:
43
+ response = f"I'm sorry, but I encountered an error: {str(e)}"
44
+
45
+ history[-1] = (history[-1][0], "")
46
+ for character in response:
47
+ if stop_generation:
48
+ break
49
+ history[-1] = (history[-1][0], history[-1][1] + character)
50
+ time.sleep(0.01)
51
+ yield history
52
+
53
+ def stop_response():
54
+ global stop_generation
55
+ stop_generation = True
56
+
57
+ def clear_chat():
58
+ global memory
59
+ memory.clear()
60
+ return []
61
+
62
+ with gr.Blocks() as demo:
63
+ with gr.Column():
64
+ chatbot = gr.Chatbot([], elem_id="chatbot")
65
+ msg = gr.Textbox(label="Message")
66
+ clear = gr.Button("Clear")
67
+
68
+ chat_msg = msg.submit(add_message, [chatbot, msg], [chatbot, msg], queue=False).then(
69
+ bot, chatbot, chatbot
70
+ )
71
+
72
+ clear.click(clear_chat, None, chatbot, queue=False)
73
+
74
+ stop_btn = gr.Button("Stop Generation")
75
+ stop_btn.click(stop_response)
76
+
77
+ demo.queue()
78
+
79
+ if __name__ == "__main__":
80
  demo.launch()