Bijay13 commited on
Commit
5ec59b0
·
verified ·
1 Parent(s): 6b0a0e8

Upload 3 files

Browse files
Files changed (3) hide show
  1. README.md +89 -13
  2. app.py +110 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -1,13 +1,89 @@
1
- ---
2
- title: Simple Chat Bot
3
- emoji: 👁
4
- colorFrom: blue
5
- colorTo: blue
6
- sdk: gradio
7
- sdk_version: 6.2.0
8
- app_file: app.py
9
- pinned: false
10
- short_description: Testing simple chat bot
11
- ---
12
-
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 🤖 LangChain Groq Chatbot
2
+
3
+ A conversational AI chatbot built with LangChain and Groq, deployable on Hugging Face Spaces.
4
+
5
+
6
+ ## Deploying to Hugging Face Spaces
7
+
8
+ ### Method 1: Using the Hugging Face Web Interface
9
+
10
+ 1. Go to [huggingface.co/spaces](https://huggingface.co/spaces)
11
+ 2. Click "Create new Space"
12
+ 3. Choose a name for your Space
13
+ 4. Select "Gradio" as the SDK
14
+ 5. Click "Create Space"
15
+ 6. Upload the following files:
16
+ - `chatbot_app.py` (rename to `app.py` for Hugging Face)
17
+ - `requirements.txt`
18
+ - `README.md`
19
+ 7. In your Space settings, go to "Settings" → "Variables and secrets"
20
+ 8. Add a new secret: `GROQ_API_KEY` with your Groq API key
21
+ 9. Your Space will automatically build and deploy!
22
+
23
+ ### Method 2: Using Git
24
+
25
+ 1. Create a new Space on Hugging Face
26
+ 2. Clone your Space repository:
27
+ ```bash
28
+ git clone https://huggingface.co/spaces/YOUR_USERNAME/YOUR_SPACE_NAME
29
+ cd YOUR_SPACE_NAME
30
+ ```
31
+
32
+ 3. Copy the files:
33
+ ```bash
34
+ cp /path/to/chatbot_app.py app.py
35
+ cp /path/to/requirements.txt .
36
+ cp /path/to/README.md .
37
+ ```
38
+
39
+ 4. Commit and push:
40
+ ```bash
41
+ git add .
42
+ git commit -m "Initial commit"
43
+ git push
44
+ ```
45
+
46
+ 5. Add your `GROQ_API_KEY` as a secret in your Space settings
47
+
48
+ ## Usage
49
+
50
+ 1. Enter your Groq API key in the text field (or use the environment variable)
51
+ 2. Type your message in the message box
52
+ 3. Click "Send" or press Enter
53
+ 4. The AI will respond to your message
54
+ 5. Click "Clear Conversation" to start a new conversation
55
+
56
+ ## Supported Models
57
+
58
+ The chatbot uses the `mixtral-8x7b-32768` model by default. You can modify the model in the code to use other Groq-supported models:
59
+
60
+ - `mixtral-8x7b-32768`
61
+ - `llama2-70b-4096`
62
+ - `gemma-7b-it`
63
+ - And more available on Groq
64
+
65
+ ## Project Structure
66
+
67
+ ```
68
+ chatbot/
69
+ ├── chatbot_app.py # Main application file (rename to app.py for HF)
70
+ ├── requirements.txt # Python dependencies
71
+ ├── README.md # This file
72
+ └── .env.example # Example environment variables
73
+ ```
74
+
75
+ ## Configuration
76
+
77
+ You can customize the chatbot by modifying these parameters in `chatbot_app.py`:
78
+
79
+ - `model_name`: Change the Groq model
80
+ - `temperature`: Control randomness (0.0 to 1.0)
81
+ - `max_tokens`: Maximum response length
82
+
83
+
84
+ ## Acknowledgments
85
+
86
+ - Built with [LangChain](https://langchain.com/)
87
+ - Powered by [Groq](https://groq.com/)
88
+ - UI created with [Gradio](https://gradio.app/)
89
+ - Hosted on [Hugging Face Spaces](https://huggingface.co/spaces)
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from langchain_groq import ChatGroq
4
+ from langchain.memory import ConversationBufferMemory
5
+ from langchain.chains import ConversationChain
6
+ from langchain.prompts import PromptTemplate
7
+
8
+ def initialize_chatbot(api_key, model_name="mixtral-8x7b-32768"):
9
+ llm = ChatGroq(
10
+ groq_api_key=api_key,
11
+ model_name=model_name,
12
+ temperature=0.7,
13
+ max_tokens=1024
14
+ )
15
+
16
+ memory = ConversationBufferMemory(
17
+ return_messages=True,
18
+ memory_key="history"
19
+ )
20
+
21
+ template = """You are a helpful AI assistant. Have a natural conversation with the user.
22
+
23
+ Current conversation:
24
+ {history}
25
+ Human: {input}
26
+ AI Assistant:"""
27
+
28
+ prompt = PromptTemplate(
29
+ input_variables=["history", "input"],
30
+ template=template
31
+ )
32
+
33
+ conversation = ConversationChain(
34
+ llm=llm,
35
+ memory=memory,
36
+ prompt=prompt,
37
+ verbose=False
38
+ )
39
+
40
+ return conversation
41
+
42
+ conversation_chain = None
43
+
44
+ def chat_function(message, history, api_key):
45
+ global conversation_chain
46
+
47
+ if not api_key:
48
+ return "Please provide a Groq API key to start chatting."
49
+
50
+ if conversation_chain is None:
51
+ try:
52
+ conversation_chain = initialize_chatbot(api_key)
53
+ except Exception as e:
54
+ return f"Error initializing chatbot: {str(e)}"
55
+
56
+ try:
57
+ response = conversation_chain.predict(input=message)
58
+ return response
59
+ except Exception as e:
60
+ return f"Error: {str(e)}"
61
+
62
+ def reset_conversation():
63
+ global conversation_chain
64
+ conversation_chain = None
65
+ return None
66
+
67
+ with gr.Blocks(title="LangChain Groq Chatbot") as demo:
68
+ gr.Markdown("# 🤖 LangChain Groq Chatbot")
69
+ gr.Markdown("Chat with an AI assistant powered by LangChain and Groq")
70
+
71
+ with gr.Row():
72
+ api_key_input = gr.Textbox(
73
+ label="Groq API Key",
74
+ placeholder="Enter your Groq API key here...",
75
+ type="password",
76
+ value=os.getenv("GROQ_API_KEY", "")
77
+ )
78
+
79
+ chatbot = gr.Chatbot(height=400)
80
+
81
+ with gr.Row():
82
+ msg = gr.Textbox(
83
+ label="Message",
84
+ placeholder="Type your message here...",
85
+ scale=4
86
+ )
87
+ submit_btn = gr.Button("Send", scale=1)
88
+
89
+ with gr.Row():
90
+ clear_btn = gr.Button("Clear Conversation")
91
+
92
+ gr.Markdown("### Instructions:")
93
+ gr.Markdown("1. Get a free API key from [Groq Console](https://console.groq.com)")
94
+ gr.Markdown("2. Enter your API key above")
95
+ gr.Markdown("3. Start chatting!")
96
+
97
+ def respond(message, chat_history, api_key):
98
+ if not message.strip():
99
+ return chat_history, ""
100
+
101
+ bot_message = chat_function(message, chat_history, api_key)
102
+ chat_history.append((message, bot_message))
103
+ return chat_history, ""
104
+
105
+ msg.submit(respond, [msg, chatbot, api_key_input], [chatbot, msg])
106
+ submit_btn.click(respond, [msg, chatbot, api_key_input], [chatbot, msg])
107
+ clear_btn.click(lambda: (reset_conversation(), None), None, chatbot)
108
+
109
+ if __name__ == "__main__":
110
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==4.44.0
2
+ langchain==0.3.7
3
+ langchain-groq==0.2.1
4
+ python-dotenv==1.0.0