medmac01 KBayoud commited on
Commit
fd6c26a
1 Parent(s): bff1dca

add source citation (#1)

Browse files

- add source citation (55060ca3070a23d52de0b912a21eb9863572057c)


Co-authored-by: Khadija Bayoud <KBayoud@users.noreply.huggingface.co>

Files changed (1) hide show
  1. app.py +76 -39
app.py CHANGED
@@ -3,6 +3,7 @@ import streamlit as st
3
  from streamlit_chat import message
4
  from streamlit_extras.colored_header import colored_header
5
  from streamlit_extras.add_vertical_space import add_vertical_space
 
6
  import requests
7
  from gradio_client import Client
8
  import datetime as dt
@@ -14,9 +15,8 @@ st.set_page_config(page_title="HugChat - An LLM-powered Streamlit app")
14
  API_TOKEN = st.secrets['HF_TOKEN']
15
  API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
16
  headers = {"Authorization": f"Bearer {str(API_TOKEN)}"}
17
- def get_text():
18
- input_text = st.text_input("You: ", "", key="input")
19
- return input_text
20
 
21
  soil_types = {
22
  "Sais plain": "Brown limestone, vertisols, lithosols, and regosols",
@@ -33,9 +33,11 @@ soil_types = {
33
  "Argan zone": "Soils are mostly lithosols and regosols, associated with fluvisols and saline soils on lowlands",
34
  "Presaharan soils": "Lithosols and regosols in association with sierozems and regs",
35
  "Saharan zone": "Yermosols, associated with sierozems, lithosols, and saline soils"
36
- }
37
-
38
 
 
 
 
39
 
40
  def get_weather_data(city):
41
  base_url = "http://api.openweathermap.org/data/2.5/weather?"
@@ -69,11 +71,8 @@ def get_weather_data(city):
69
  print(f"Error: {e}")
70
  return None
71
 
72
- # Example usage
73
-
74
-
75
- def query(payload):
76
- response = requests.post(API_URL, headers=headers, json=payload)
77
  return response.json()
78
 
79
  def translate(text,source="English",target="Moroccan Arabic"):
@@ -87,9 +86,48 @@ def translate(text,source="English",target="Moroccan Arabic"):
87
  print(result)
88
  return result
89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90
 
91
  # Function to generate a response from the chatbot
92
- def generate_response(user_input,region):
93
 
94
  city = "Fez"
95
  weather_info = get_weather_data(city)
@@ -97,8 +135,8 @@ def generate_response(user_input,region):
97
  print(weather_info)
98
 
99
  user_input_translated = str(translate(user_input, "Moroccan Arabic", "English"))
100
- name = 'Fellah(a)'
101
- date = 'December'
102
  location = 'Fes, Morocco'
103
  soil_type = soil_types[region] # Use the selected region's soil type
104
  humidity = weather_info["humidity"]
@@ -111,29 +149,16 @@ def generate_response(user_input,region):
111
 
112
  instruction = f'''
113
  <s> [INST] You are an agriculture expert, and my name is {name} Given the following informations, prevailing weather conditions, specific land type, chosen type of agriculture, and soil composition of a designated area, answer the question below
114
- Location: {location},
115
- Current Month : {date}
116
- land type: {soil_types[region]}
117
- humidity: {humidity}
118
- weather: {weather}
119
- temperature: {temp}
120
- Question: {user_input_translated}[/INST]</s>
121
- '''
122
- prompt = f'''
123
- You are an agriculture expert, Given the following informations, geographical coordinates (latitude and longitude), prevailing weather conditions, specific land type, chosen type of agriculture, and soil composition of a designated area, request the LLM to provide detailed insights and predictions on optimal agricultural practices, potential crop yields, and recommended soil management strategies, or answer the question below
124
- Location: {location},
125
- land type: {soil_type}
126
- humidity: {humidity}
127
- weather: {weather}
128
- temperature: {temp}
129
  '''
130
- # output = query({"inputs": f'''
131
- # PROMPT: {prompt}
132
- # QUESTION: {user_input}
133
- # ANSWER:
134
- # ''',})
135
 
136
- output = query({"inputs": instruction, "parameters":{"max_new_tokens":250, "temperature":1, "return_full_text":False}})
137
  # print(headers)
138
  print(instruction)
139
  print(output)
@@ -165,10 +190,7 @@ def main():
165
  💡 Note: No API key required!
166
  ''')
167
  add_vertical_space(5)
168
- st.write('Made with ❤️ by [llama-crew](https://huggingface.co/llama-crew)')
169
- #st.write('Made with ❤️ by [llama-crew](https://hf.co/medmac01)')
170
-
171
-
172
 
173
  # Generate empty lists for generated and past.
174
  ## generated stores AI generated responses
@@ -189,6 +211,8 @@ def main():
189
 
190
  colored_header(label='', description='', color_name='blue-30')
191
  response_container = st.container()
 
 
192
 
193
  # User input
194
  ## Function for taking user provided prompt as input
@@ -203,7 +227,7 @@ def main():
203
  ## Conditional display of AI generated responses as a function of user provided prompts
204
  with response_container:
205
  if user_input:
206
- response = generate_response(user_input,str(selected_region))
207
  st.session_state.past.append(user_input)
208
  st.session_state.generated.append(response)
209
 
@@ -212,5 +236,18 @@ def main():
212
  message(st.session_state['past'][i], is_user=True, key=str(i) + '_user', logo="https://i.pinimg.com/originals/d5/b2/13/d5b21384ccaaa6f9ef32986f17c50638.png")
213
  message(st.session_state["generated"][i], key=str(i), logo= "https://emojiisland.com/cdn/shop/products/Robot_Emoji_Icon_7070a254-26f7-4a54-8131-560e38e34c2e_large.png?v=1571606114")
214
 
 
 
 
 
 
 
 
 
 
 
 
 
 
215
  if __name__ == "__main__":
216
  main()
 
3
  from streamlit_chat import message
4
  from streamlit_extras.colored_header import colored_header
5
  from streamlit_extras.add_vertical_space import add_vertical_space
6
+ from datetime import datetime
7
  import requests
8
  from gradio_client import Client
9
  import datetime as dt
 
15
  API_TOKEN = st.secrets['HF_TOKEN']
16
  API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
17
  headers = {"Authorization": f"Bearer {str(API_TOKEN)}"}
18
+
19
+ API_URL1 = "https://api-inference.huggingface.co/models/HuggingFaceH4/zephyr-7b-beta"
 
20
 
21
  soil_types = {
22
  "Sais plain": "Brown limestone, vertisols, lithosols, and regosols",
 
33
  "Argan zone": "Soils are mostly lithosols and regosols, associated with fluvisols and saline soils on lowlands",
34
  "Presaharan soils": "Lithosols and regosols in association with sierozems and regs",
35
  "Saharan zone": "Yermosols, associated with sierozems, lithosols, and saline soils"
36
+ }
 
37
 
38
+ def get_text():
39
+ input_text = st.text_input("You: ", "", key="input")
40
+ return input_text
41
 
42
  def get_weather_data(city):
43
  base_url = "http://api.openweathermap.org/data/2.5/weather?"
 
71
  print(f"Error: {e}")
72
  return None
73
 
74
+ def query(payload, api_url):
75
+ response = requests.post(api_url, headers=headers, json=payload)
 
 
 
76
  return response.json()
77
 
78
  def translate(text,source="English",target="Moroccan Arabic"):
 
86
  print(result)
87
  return result
88
 
89
+ def search_url(search_query):
90
+ API_KEY = st.secrets['API_TOKEN']
91
+ SEARCH_ENGINE_ID = st.secrets['SEARCH_ENGINE_ID']
92
+
93
+ url = 'https://www.googleapis.com/customsearch/v1'
94
+
95
+ params = {
96
+ 'q': search_query,
97
+ 'key': API_KEY,
98
+ 'cx': SEARCH_ENGINE_ID,
99
+ }
100
+
101
+ response = requests.get(url, params=params)
102
+
103
+ results = response.json()
104
+
105
+ # print(results)
106
+
107
+ if 'items' in results:
108
+ for i in range(min(5, len(results['items']))):
109
+ print(f"Link {i + 1}: {results['items'][i]['link']}")
110
+ return results['items'][:5]
111
+ else:
112
+ print("No search results found.")
113
+ return None
114
+
115
+ def get_search_query(response):
116
+ instruction = f'''
117
+ Based on these information, generate a short summarized search terms. Don't include weather specifications.
118
+ Information : {response}
119
+ Search term keyword:
120
+ '''
121
+
122
+ output = query({"inputs": instruction, "parameters":{"max_new_tokens":40, "temperature":.3, "return_full_text":False}}, API_URL1)
123
+ print(instruction)
124
+ print(output)
125
+ ss = output[0]['generated_text'][:output[0]['generated_text'].find('\n')]
126
+ print(ss)
127
+ return ss
128
 
129
  # Function to generate a response from the chatbot
130
+ def generate_response(user_input, region, date):
131
 
132
  city = "Fez"
133
  weather_info = get_weather_data(city)
 
135
  print(weather_info)
136
 
137
  user_input_translated = str(translate(user_input, "Moroccan Arabic", "English"))
138
+ name = 'Fellah'
139
+ date = date
140
  location = 'Fes, Morocco'
141
  soil_type = soil_types[region] # Use the selected region's soil type
142
  humidity = weather_info["humidity"]
 
149
 
150
  instruction = f'''
151
  <s> [INST] You are an agriculture expert, and my name is {name} Given the following informations, prevailing weather conditions, specific land type, chosen type of agriculture, and soil composition of a designated area, answer the question below
152
+ Location: {location},
153
+ Current Month : {date}
154
+ land type: {soil_types[region]}
155
+ humidity: {humidity}
156
+ weather: {weather}
157
+ temperature: {temp}
158
+ Question: {user_input_translated}[/INST]</s>
 
 
 
 
 
 
 
 
159
  '''
 
 
 
 
 
160
 
161
+ output = query({"inputs": instruction, "parameters":{"max_new_tokens":250, "temperature":1, "return_full_text":False}}, API_URL)
162
  # print(headers)
163
  print(instruction)
164
  print(output)
 
190
  💡 Note: No API key required!
191
  ''')
192
  add_vertical_space(5)
193
+ st.write('Made with ❤️ by [llama-crew](https://huggingface.co/smart-fellah)')
 
 
 
194
 
195
  # Generate empty lists for generated and past.
196
  ## generated stores AI generated responses
 
211
 
212
  colored_header(label='', description='', color_name='blue-30')
213
  response_container = st.container()
214
+
215
+ date = datetime.now().month
216
 
217
  # User input
218
  ## Function for taking user provided prompt as input
 
227
  ## Conditional display of AI generated responses as a function of user provided prompts
228
  with response_container:
229
  if user_input:
230
+ response = generate_response(user_input,str(selected_region), str(date))
231
  st.session_state.past.append(user_input)
232
  st.session_state.generated.append(response)
233
 
 
236
  message(st.session_state['past'][i], is_user=True, key=str(i) + '_user', logo="https://i.pinimg.com/originals/d5/b2/13/d5b21384ccaaa6f9ef32986f17c50638.png")
237
  message(st.session_state["generated"][i], key=str(i), logo= "https://emojiisland.com/cdn/shop/products/Robot_Emoji_Icon_7070a254-26f7-4a54-8131-560e38e34c2e_large.png?v=1571606114")
238
 
239
+ # Add Google icon button to retrieve links
240
+ if st.button(f"Double-Check Response", key=f"google_button_{i}"):
241
+ search_query = get_search_query(st.session_state['generated'][i])
242
+ retrieved_links = search_url(search_query)
243
+ if retrieved_links:
244
+ st.markdown("**Google Search Results:**")
245
+ for j, link in enumerate(retrieved_links):
246
+ st.markdown(f"{j + 1}. [{link['title']}]({link['link']})")
247
+
248
+ # Display Google logo
249
+ google_logo_url = "https://www.gstatic.com/webp/gallery/2.jpg"
250
+ st.image(google_logo_url, width=50, caption="Google Logo")
251
+
252
  if __name__ == "__main__":
253
  main()