Klaus04 commited on
Commit
9c22e84
Β·
verified Β·
1 Parent(s): 8c6f23b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -59
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 os
6
- import gradio as gr
7
- import numpy as np
8
- import pickle
9
-
10
- model = SentenceTransformer("all-MiniLM-L6-v2")
11
- index = faiss.read_index("solar_vectors.index")
12
- with open("chunks.pkl", "rb") as f:
13
- chunks= pickle.load(f)
14
-
15
- os.environ["GROQ_API_KEY"]= "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"
16
- llm = ChatGroq(model="mixtral-8x7b-32768",temperature=0)
17
-
18
- def retrieve_relevant_text(query, top_k=1):
19
- query_embedding = model.encode([query])
20
- distances, indices = index.search(np.array(query_embedding), top_k)
21
- return [chunks[i] for i in indices[0]]
22
-
23
- def generate_response(user_query):
24
- retrieved_text = retrieve_relevant_text(user_query, top_k=4)
25
- 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)."
26
- prompt_template = ChatPromptTemplate.from_messages([
27
- ("system", system_message),
28
- ("human", f"Use the following information to answer: {retrieved_text} \n\nUser Query: {user_query}")
29
- ])
30
-
31
- chain = prompt_template | llm
32
- response = chain.invoke({"text": user_query})
33
- return response.content
34
-
35
- def gradio_chatbot(user_input):
36
- response = generate_response(user_input)
37
- return response
38
-
39
-
40
-
41
- with gr.Blocks() as demo:
42
- gr.Markdown("# 🌞 SolarAI 🌞")
43
-
44
- with gr.Row():
45
- user_input = gr.Textbox(
46
- placeholder="Ask me anything about solar energy...",
47
- lines=2,
48
- interactive=True
49
- )
50
-
51
- with gr.Row():
52
- output_box = gr.Textbox(
53
- lines=12,
54
- interactive=True,
55
- label="Chatbot Response"
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()