Update app.py
Browse files
app.py
CHANGED
@@ -6,20 +6,41 @@ from transformers import pipeline
|
|
6 |
import numpy as np
|
7 |
import tempfile
|
8 |
import openai
|
9 |
-
import
|
|
|
10 |
import streamlit as st
|
11 |
from PIL import Image
|
12 |
from gtts import gTTS
|
13 |
from io import BytesIO
|
|
|
14 |
|
15 |
# Pinecone and OpenAI setup
|
16 |
-
|
17 |
-
openai.api_key = os.getenv("
|
18 |
|
19 |
-
#
|
|
|
|
|
|
|
20 |
index_name = "farming-assistant"
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
master_prompt = """
|
25 |
As a Natural Farming Fertilizers Assistant, you will assist the user with any farming-related question, always willing to answer any question and provide useful organic farming advice in the following format.
|
@@ -40,8 +61,12 @@ def generate_response(question):
|
|
40 |
)["data"][0]["embedding"]
|
41 |
|
42 |
# Query Pinecone for relevant documents
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
|
46 |
# Extract relevant information
|
47 |
contexts = [match["metadata"]["text"] for match in query_result["matches"]]
|
@@ -58,6 +83,13 @@ def generate_response(question):
|
|
58 |
|
59 |
return response
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
def launch_bot():
|
62 |
if 'cfg' not in st.session_state:
|
63 |
questions = list(eval(os.environ['examples']))
|
@@ -75,7 +107,7 @@ def launch_bot():
|
|
75 |
|
76 |
# Left side content
|
77 |
with st.sidebar:
|
78 |
-
image = Image.open('
|
79 |
st.markdown(f"## Welcome to {cfg.title}\n\n"
|
80 |
f"This demo uses an AI organic farming expert and carefully curated library system to achieve greater accuracy in agronomics and agricultural methodology. Created by Copyleft Cultivars, a nonprofit, we hope you enjoy this beta-test early access version.\n\n")
|
81 |
|
|
|
6 |
import numpy as np
|
7 |
import tempfile
|
8 |
import openai
|
9 |
+
from pinecone.grpc import PineconeGRPC as Pinecone # Corrected Pinecone import
|
10 |
+
from pinecone import ServerlessSpec
|
11 |
import streamlit as st
|
12 |
from PIL import Image
|
13 |
from gtts import gTTS
|
14 |
from io import BytesIO
|
15 |
+
import time # For delay during index readiness check
|
16 |
|
17 |
# Pinecone and OpenAI setup
|
18 |
+
pinecone_api_key = os.getenv("PINECONE_API_KEY")
|
19 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
20 |
|
21 |
+
# Initialize Pinecone client
|
22 |
+
pc = Pinecone(api_key=pinecone_api_key)
|
23 |
+
|
24 |
+
# Create or retrieve Pinecone index
|
25 |
index_name = "farming-assistant"
|
26 |
+
dimension = 1536 # Adjust dimension to match OpenAI embeddings
|
27 |
+
|
28 |
+
if not pc.has_index(index_name):
|
29 |
+
pc.create_index(
|
30 |
+
name=index_name,
|
31 |
+
dimension=dimension,
|
32 |
+
metric="cosine",
|
33 |
+
spec=ServerlessSpec(
|
34 |
+
cloud='aws',
|
35 |
+
region='us-east-1'
|
36 |
+
)
|
37 |
+
)
|
38 |
+
|
39 |
+
# Wait for the index to be ready
|
40 |
+
while not pc.describe_index(index_name).status['ready']:
|
41 |
+
time.sleep(1)
|
42 |
+
|
43 |
+
index = pc.Index(index_name) # Corrected method to connect to the index
|
44 |
|
45 |
master_prompt = """
|
46 |
As a Natural Farming Fertilizers Assistant, you will assist the user with any farming-related question, always willing to answer any question and provide useful organic farming advice in the following format.
|
|
|
61 |
)["data"][0]["embedding"]
|
62 |
|
63 |
# Query Pinecone for relevant documents
|
64 |
+
query_result = index.query(
|
65 |
+
vector=query_embedding,
|
66 |
+
top_k=5,
|
67 |
+
include_metadata=True,
|
68 |
+
namespace="farming-assistant"
|
69 |
+
)
|
70 |
|
71 |
# Extract relevant information
|
72 |
contexts = [match["metadata"]["text"] for match in query_result["matches"]]
|
|
|
83 |
|
84 |
return response
|
85 |
|
86 |
+
def upsert_vectors(vectors):
|
87 |
+
# Upsert vectors into Pinecone index
|
88 |
+
index.upsert(
|
89 |
+
vectors=vectors,
|
90 |
+
namespace="farming-assistant"
|
91 |
+
)
|
92 |
+
|
93 |
def launch_bot():
|
94 |
if 'cfg' not in st.session_state:
|
95 |
questions = list(eval(os.environ['examples']))
|
|
|
107 |
|
108 |
# Left side content
|
109 |
with st.sidebar:
|
110 |
+
image = Image.open('Pinecone-logo.png') # Update with appropriate logo
|
111 |
st.markdown(f"## Welcome to {cfg.title}\n\n"
|
112 |
f"This demo uses an AI organic farming expert and carefully curated library system to achieve greater accuracy in agronomics and agricultural methodology. Created by Copyleft Cultivars, a nonprofit, we hope you enjoy this beta-test early access version.\n\n")
|
113 |
|