Update app.py
Browse files
app.py
CHANGED
|
@@ -1,60 +1,58 @@
|
|
| 1 |
-
import faiss
|
| 2 |
-
from sentence_transformers import SentenceTransformer
|
| 3 |
-
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
-
from langchain_groq import ChatGroq
|
| 5 |
-
import
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
gr.
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
submit_btn = gr.Button("Ask")
|
| 59 |
-
submit_btn.click(fn=gradio_chatbot, inputs=user_input, outputs=output_box)
|
| 60 |
demo.launch()
|
|
|
|
| 1 |
+
import faiss
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 4 |
+
from langchain_groq import ChatGroq
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
import pickle
|
| 8 |
+
|
| 9 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 10 |
+
index = faiss.read_index("solar_vectors.index")
|
| 11 |
+
with open("chunks.pkl", "rb") as f:
|
| 12 |
+
chunks= pickle.load(f)
|
| 13 |
+
|
| 14 |
+
llm = ChatGroq(model="mixtral-8x7b-32768",temperature=0.2)
|
| 15 |
+
|
| 16 |
+
def retrieve_relevant_text(query, top_k=1):
|
| 17 |
+
query_embedding = model.encode([query])
|
| 18 |
+
distances, indices = index.search(np.array(query_embedding), top_k)
|
| 19 |
+
return [chunks[i] for i in indices[0]]
|
| 20 |
+
|
| 21 |
+
def generate_response(user_query):
|
| 22 |
+
retrieved_text = retrieve_relevant_text(user_query, top_k=4)
|
| 23 |
+
system_message = "You are an intelligent assistant that provides accurate, helpful information about solar energy based on the information provided(if not, answer according to your knowledge)."
|
| 24 |
+
prompt_template = ChatPromptTemplate.from_messages([
|
| 25 |
+
("system", system_message),
|
| 26 |
+
("human", f"Use the following information to answer: {retrieved_text} \n\nUser Query: {user_query}")
|
| 27 |
+
])
|
| 28 |
+
|
| 29 |
+
chain = prompt_template | llm
|
| 30 |
+
response = chain.invoke({"text": user_query})
|
| 31 |
+
return response.content
|
| 32 |
+
|
| 33 |
+
def gradio_chatbot(user_input):
|
| 34 |
+
response = generate_response(user_input)
|
| 35 |
+
return response
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
with gr.Blocks() as demo:
|
| 40 |
+
gr.Markdown("# π SolarAI π")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
user_input = gr.Textbox(
|
| 44 |
+
placeholder="Ask me anything about solar energy...",
|
| 45 |
+
lines=2,
|
| 46 |
+
interactive=True
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
output_box = gr.Textbox(
|
| 51 |
+
lines=12,
|
| 52 |
+
interactive=True,
|
| 53 |
+
label="Chatbot Response"
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
submit_btn = gr.Button("Ask")
|
| 57 |
+
submit_btn.click(fn=gradio_chatbot, inputs=user_input, outputs=output_box)
|
|
|
|
|
|
|
| 58 |
demo.launch()
|