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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 9 |
pinned: false
|
| 10 |
short_description: It is an AI-powered chatbot.
|
| 11 |
---
|
| 12 |
+
# π Solar Industry Chatbot
|
| 13 |
+
|
| 14 |
+
This project is an **AI-powered chatbot** that provides accurate and insightful information about the **solar industry**, including **solar panel technology, installation processes, maintenance, costs, ROI analysis, and market trends**. The chatbot integrates **LLM (ChatGroq - Mixtral-8x7B)** with **vector search (FAISS)** for better context-aware responses.
|
| 15 |
+
|
| 16 |
+
## π Features
|
| 17 |
+
|
| 18 |
+
- Extracts solar energy knowledge from a **DOCX file**
|
| 19 |
+
- Converts text into **embeddings** using `SentenceTransformer`
|
| 20 |
+
- Stores embeddings in a **FAISS vector database** for efficient retrieval
|
| 21 |
+
- Queries relevant information before sending it to **ChatGroq (Mixtral-8x7B)**
|
| 22 |
+
- Provides an **interactive chatbot UI using Gradio**
|
| 23 |
+
|
| 24 |
+
---
|
| 25 |
+
|
| 26 |
+
## π Installation & Setup
|
| 27 |
+
|
| 28 |
+
### **Step 1: Install Dependencies**
|
| 29 |
+
|
| 30 |
+
```bash
|
| 31 |
+
pip install -r requirements.txt
|
| 32 |
+
```
|
| 33 |
+
|
| 34 |
+
### **Step 2: Run the Chatbot**
|
| 35 |
+
|
| 36 |
+
```bash
|
| 37 |
+
python app.py
|
| 38 |
+
```
|
| 39 |
+
|
| 40 |
+
---
|
| 41 |
+
|
| 42 |
+
## π Code Breakdown (Function-by-Function)
|
| 43 |
+
|
| 44 |
+
### **1οΈβ£ Extracting Text from DOCX**
|
| 45 |
+
|
| 46 |
+
```python
|
| 47 |
+
def extract_text_from_docx(file_path):
|
| 48 |
+
doc = Document(file_path)
|
| 49 |
+
text = "\n".join([para.text for para in doc.paragraphs if para.text.strip()])
|
| 50 |
+
return text
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
πΉ **Purpose:** Reads a `.docx` file and extracts useful solar-related information.
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
### **2οΈβ£ Splitting Text into Chunks**
|
| 58 |
+
|
| 59 |
+
```python
|
| 60 |
+
def split_text(text, chunk_size=300):
|
| 61 |
+
sentences = text.split(". ")
|
| 62 |
+
chunks, current_chunk = [], ""
|
| 63 |
+
for sentence in sentences:
|
| 64 |
+
if len(current_chunk) + len(sentence) < chunk_size:
|
| 65 |
+
current_chunk += sentence + ". "
|
| 66 |
+
else:
|
| 67 |
+
chunks.append(current_chunk.strip())
|
| 68 |
+
current_chunk = sentence + ". "
|
| 69 |
+
if current_chunk:
|
| 70 |
+
chunks.append(current_chunk.strip())
|
| 71 |
+
return chunks
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
πΉ **Purpose:** Splits large text data into smaller, meaningful **chunks** for better vector search performance.
|
| 75 |
+
|
| 76 |
+
---
|
| 77 |
+
|
| 78 |
+
### **3οΈβ£ Generating Embeddings**
|
| 79 |
+
|
| 80 |
+
```python
|
| 81 |
+
from sentence_transformers import SentenceTransformer
|
| 82 |
+
|
| 83 |
+
model = SentenceTransformer("all-MiniLM-L6-v2") # Embedding model
|
| 84 |
+
embeddings = model.encode(chunks)
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
πΉ **Purpose:** Converts text **chunks** into numerical representations (vectors) for similarity search.
|
| 88 |
+
|
| 89 |
+
---
|
| 90 |
+
|
| 91 |
+
### **4οΈβ£ Storing Embeddings in FAISS Vector Database**
|
| 92 |
+
|
| 93 |
+
```python
|
| 94 |
+
import faiss
|
| 95 |
+
import numpy as np
|
| 96 |
+
|
| 97 |
+
vector_dim = embeddings.shape[1]
|
| 98 |
+
index = faiss.IndexFlatL2(vector_dim)
|
| 99 |
+
index.add(np.array(embeddings))
|
| 100 |
+
faiss.write_index(index, "solar_vectors.index")
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
πΉ **Purpose:** Uses **FAISS** to efficiently store and retrieve relevant text when a user asks a question.
|
| 104 |
+
|
| 105 |
+
---
|
| 106 |
+
|
| 107 |
+
### **5οΈβ£ Retrieving Relevant Information**
|
| 108 |
+
|
| 109 |
+
```python
|
| 110 |
+
def retrieve_relevant_text(query, top_k=2):
|
| 111 |
+
query_embedding = model.encode([query])
|
| 112 |
+
distances, indices = index.search(np.array(query_embedding), top_k)
|
| 113 |
+
return " ".join([chunks[i] for i in indices[0]])
|
| 114 |
+
```
|
| 115 |
+
|
| 116 |
+
πΉ **Purpose:** Finds the **most relevant** pieces of information to **pass to the chatbot** before generating a response.
|
| 117 |
+
|
| 118 |
+
---
|
| 119 |
+
|
| 120 |
+
### **6οΈβ£ Chatbot Integration with ChatGroq (Mixtral-8x7B)**
|
| 121 |
+
|
| 122 |
+
```python
|
| 123 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 124 |
+
from langchain_groq import ChatGroq
|
| 125 |
+
|
| 126 |
+
llm = ChatGroq(model="mixtral-8x7b-32768", temperature=0.2)
|
| 127 |
+
|
| 128 |
+
def chat_with_groq(user_query):
|
| 129 |
+
retrieved_text = retrieve_relevant_text(user_query)
|
| 130 |
+
system_message = "You are an AI assistant that provides accurate solar energy information."
|
| 131 |
+
prompt_template = ChatPromptTemplate.from_messages([
|
| 132 |
+
("system", system_message),
|
| 133 |
+
("human", f"Use the following information to answer: {retrieved_text} \n\nUser Query: {user_query}")
|
| 134 |
+
])
|
| 135 |
+
chain = prompt_template | llm
|
| 136 |
+
response = chain.invoke({"text": user_query})
|
| 137 |
+
return response.content
|
| 138 |
+
```
|
| 139 |
+
|
| 140 |
+
πΉ **Purpose:** Uses **retrieved data + user query** to generate an **LLM-based response**.
|
| 141 |
+
|
| 142 |
+
---
|
| 143 |
+
|
| 144 |
+
### **7οΈβ£ Gradio Chatbot UI**
|
| 145 |
+
|
| 146 |
+
```python
|
| 147 |
+
import gradio as gr
|
| 148 |
+
|
| 149 |
+
def gradio_chatbot(user_input):
|
| 150 |
+
response = chat_with_groq(user_input)
|
| 151 |
+
return response
|
| 152 |
+
|
| 153 |
+
with gr.Blocks() as demo:
|
| 154 |
+
gr.Markdown("# π SolarAI π")
|
| 155 |
+
with gr.Row():
|
| 156 |
+
user_input = gr.Textbox(placeholder="Ask me anything about solar energy...", lines=2, interactive=True)
|
| 157 |
+
with gr.Row():
|
| 158 |
+
output_box = gr.Textbox(lines=6, interactive=True, label="Chatbot Response")
|
| 159 |
+
submit_btn = gr.Button("Ask")
|
| 160 |
+
submit_btn.click(fn=gradio_chatbot, inputs=user_input, outputs=output_box)
|
| 161 |
+
|
| 162 |
+
demo.launch()
|
| 163 |
+
```
|
| 164 |
+
|
| 165 |
+
πΉ **Purpose:** Creates a **Gradio-powered UI** for user interaction with the chatbot.
|
| 166 |
+
|
| 167 |
+
---
|
| 168 |
+
|
| 169 |
+
## π Deployment Guide
|
| 170 |
+
|
| 171 |
+
### **Option 1: Run Locally**
|
| 172 |
+
|
| 173 |
+
```bash
|
| 174 |
+
python app.py
|
| 175 |
+
```
|
| 176 |
+
|
| 177 |
+
### **Option 2: Deploy on Hugging Face Spaces**
|
| 178 |
+
|
| 179 |
+
1. Create `requirements.txt`.
|
| 180 |
+
|
| 181 |
+
2. Push to Hugging Face:
|
| 182 |
+
|
| 183 |
+
```bash
|
| 184 |
+
git init
|
| 185 |
+
git add .
|
| 186 |
+
git commit -m "Deploy Solar Chatbot"
|
| 187 |
+
git remote add origin https://huggingface.co/spaces/YOUR_USERNAME/solar-chatbot
|
| 188 |
+
git push origin main
|
| 189 |
+
```
|
| 190 |
+
|
| 191 |
+
β
Your chatbot is now **LIVE** on Hugging Face Spaces!
|
| 192 |
+
|
| 193 |
+
---
|
| 194 |
+
|
| 195 |
+
## π― Future Improvements
|
| 196 |
+
|
| 197 |
+
β
Add **voice-based interaction** ποΈ β
Improve **multi-turn conversation memory** β
Enable **real-time solar industry data fetching** β
Integrate **WhatsApp/Telegram bot support** π²
|
| 198 |
+
|
| 199 |
+
π **Enjoy your Solar Industry AI Assistant!** βοΈ
|
| 200 |
+
|
| 201 |
+
|
| 202 |
|
| 203 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|