Dharun72 commited on
Commit
a27e27f
·
verified ·
1 Parent(s): f8f6243

Upload 3 files

Browse files
Files changed (3) hide show
  1. .env +1 -0
  2. chatbot.py +60 -0
  3. requirements.txt +10 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ GROQ_API_KEY='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'
chatbot.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain_groq
2
+ langchain_community
3
+ langchain
4
+ langchain_core
5
+ transformers
6
+ gradio
7
+ python-dotenv
8
+ faiss-cpu
9
+ pypdf
10
+ sentence_transformers