Update app.py
Browse files
app.py
CHANGED
@@ -80,126 +80,6 @@ Relevant documents:
|
|
80 |
{docs_context}
|
81 |
|
82 |
Current question: {question}
|
83 |
-
{master_prompt}"""
|
84 |
-
|
85 |
-
response = client.chat.completions.create(
|
86 |
-
model="meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo",
|
87 |
-
messages=[
|
88 |
-
{"role": "system", "content": master_prompt},
|
89 |
-
{"role": "user", "content": full_prompt}
|
90 |
-
],
|
91 |
-
)
|
92 |
-
|
93 |
-
return response.choices[0].message.content
|
94 |
-
|
95 |
-
def launch_bot():
|
96 |
-
if 'cfg' not in st.session_state:
|
97 |
-
questions = list(eval(os.environ['examples']))
|
98 |
-
cfg = OmegaConf.create({
|
99 |
-
'api_key': together_api_key,
|
100 |
-
'title': os.environ['title'],
|
101 |
-
'description': os.environ['description'],
|
102 |
-
'examples': questions,
|
103 |
-
'source_data_desc': os.environ['source_data_desc']
|
104 |
-
})
|
105 |
-
st.session_state.cfg = cfg
|
106 |
-
|
107 |
-
cfg = st.session_state.cfg
|
108 |
-
st.set_page_config(page_title=cfg.title, layout="wide")
|
109 |
-
|
110 |
-
# Left side content
|
111 |
-
with st.sidebar:
|
112 |
-
image = Image.open('Vectara-logo.png')
|
113 |
-
st.markdown(f"## Welcome to {cfg.title}\n\n"
|
114 |
-
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")
|
115 |
-
|
116 |
-
st.markdown("---")
|
117 |
-
st.markdown(
|
118 |
-
"## Democratizing access to farming knowledge.\n"
|
119 |
-
"This app was built with the support of our Patreon subscribers. Thank you! [Click here to join our patreon or upgrade your membership.](https://www.patreon.com/CopyleftCultivarsNonprofit). \n"
|
120 |
-
"Use of this app indicates agreement to our terms of membership, available on Copyleftcultivars.com. \n"
|
121 |
-
)
|
122 |
-
st.markdown("---")
|
123 |
-
st.image(image, width=250)
|
124 |
-
|
125 |
-
st.markdown(f"<center> <h2> Copyleft Cultivars AI Agriculture Assistant demo: {cfg.title} </h2> </center>", unsafe_allow_html=True)
|
126 |
-
st.markdown(f"<center> <h4> {cfg.description} <h4> </center>", unsafe_allow_html=True)
|
127 |
-
|
128 |
-
if "messages" not in st.session_state:
|
129 |
-
st.session_state.messages = [{"role": "assistant", "content": "How may I help you?"}]
|
130 |
-
|
131 |
-
# Display chat messages
|
132 |
-
for message in st.session_state.messages:
|
133 |
-
with st.chat_message(message["role"]):
|
134 |
-
st.write(message["content"])
|
135 |
-
|
136 |
-
# User-provided prompt
|
137 |
-
if prompt := st.chat_input():
|
138 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
139 |
-
with st.chat_message("user"):
|
140 |
-
st.write(prompt)
|
141 |
-
if any(variant in prompt for variant in ("JSON", "json", "jsON", "jSon", "Json", "jsoN", "JSon")):
|
142 |
-
if "ADMINISTRATION" not in prompt:
|
143 |
-
message = {"role": "assistant", "content": denial_response}
|
144 |
-
st.session_state.messages.append(message)
|
145 |
-
st.chat_message("assistant")
|
146 |
-
st.write(denial_response)
|
147 |
-
|
148 |
-
# Generate a new response if the last message is not from assistant
|
149 |
-
if st.session_state.messages[-1]["role"] != "assistant":
|
150 |
-
with st.chat_message("assistant"):
|
151 |
-
with st.spinner("Thinking..."):
|
152 |
-
prompt2 = prompt + master_prompt
|
153 |
-
response = generate_response(prompt2)
|
154 |
-
st.write(response)
|
155 |
-
|
156 |
-
message = {"role": "assistant", "content": response}
|
157 |
-
st.session_state.messages.append(message)
|
158 |
-
|
159 |
-
text = " :blue[Convert to Audio ] 🔊 "
|
160 |
-
# Converts Response to Audio
|
161 |
-
with st.expander(text, expanded=False):
|
162 |
-
sound_file = BytesIO()
|
163 |
-
tts = gTTS(response, lang='en')
|
164 |
-
tts.write_to_fp(sound_file)
|
165 |
-
st.audio(sound_file)
|
166 |
-
|
167 |
-
st.markdown("[Sign up for Premium](https://www.patreon.com/CopyleftCultivarsNonprofit)", unsafe_allow_html=True)
|
168 |
-
|
169 |
-
if __name__ == "__main__":
|
170 |
-
launch_bot() pc.create_index(
|
171 |
-
name=index_name,
|
172 |
-
dimension=dimension,
|
173 |
-
metric="cosine",
|
174 |
-
spec=ServerlessSpec(
|
175 |
-
cloud='aws',
|
176 |
-
region='us-east-1'
|
177 |
-
)
|
178 |
-
)
|
179 |
-
|
180 |
-
# Wait for the index to be ready
|
181 |
-
while not pc.describe_index(index_name).status['ready']:
|
182 |
-
time.sleep(1)
|
183 |
-
|
184 |
-
index = pc.Index(index_name)
|
185 |
-
|
186 |
-
denial_response = "Database scraping is not permitted. Please abide by the terms of membership, and reach out with any collaboration requests via email"
|
187 |
-
|
188 |
-
# Initialize Together AI client
|
189 |
-
client = Together(api_key=together_api_key)
|
190 |
-
|
191 |
-
@weave.op()
|
192 |
-
def generate_response(question):
|
193 |
-
# Get recent conversation context from session state
|
194 |
-
recent_messages = st.session_state.messages[-5:] # Get last 5 messages
|
195 |
-
conversation_context = "\n".join([f"{m['role']}: {m['content']}" for m in recent_messages])
|
196 |
-
|
197 |
-
# Combine context with current question
|
198 |
-
full_prompt = f"""Previous conversation:
|
199 |
-
{conversation_context}
|
200 |
-
|
201 |
-
Current question: {question}
|
202 |
-
|
203 |
{master_prompt}"""
|
204 |
|
205 |
response = client.chat.completions.create(
|
|
|
80 |
{docs_context}
|
81 |
|
82 |
Current question: {question}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
{master_prompt}"""
|
84 |
|
85 |
response = client.chat.completions.create(
|