medmac01 commited on
Commit
e42f508
1 Parent(s): 1139e6c

Upload 2 files

Browse files
Files changed (1) hide show
  1. app.py +95 -0
app.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ import os
4
+ import requests
5
+ from gradio_client import Client
6
+
7
+ API_TOKEN = os.getenv('API_TOKEN')
8
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1"
9
+ headers = {"Authorization": "Bearer {API_TOKEN}"}
10
+
11
+ def query(payload):
12
+ response = requests.post(API_URL, headers=headers, json=payload)
13
+ return response.json()
14
+
15
+ def translate(text,source="English",target="Moroccan Arabic"):
16
+ client = Client("https://facebook-seamless-m4t-v2-large.hf.space/--replicas/6w5sk/")
17
+ result = client.predict(
18
+ text, # str in 'Input text' Textbox component
19
+ source, # Literal[Afrikaans, Amharic, Armenian, Assamese, Basque, Belarusian, Bengali, Bosnian, Bulgarian, Burmese, Cantonese, Catalan, Cebuano, Central Kurdish, Croatian, Czech, Danish, Dutch, Egyptian Arabic, English, Estonian, Finnish, French, Galician, Ganda, Georgian, German, Greek, Gujarati, Halh Mongolian, Hebrew, Hindi, Hungarian, Icelandic, Igbo, Indonesian, Irish, Italian, Japanese, Javanese, Kannada, Kazakh, Khmer, Korean, Kyrgyz, Lao, Lithuanian, Luo, Macedonian, Maithili, Malayalam, Maltese, Mandarin Chinese, Marathi, Meitei, Modern Standard Arabic, Moroccan Arabic, Nepali, North Azerbaijani, Northern Uzbek, Norwegian Bokmål, Norwegian Nynorsk, Nyanja, Odia, Polish, Portuguese, Punjabi, Romanian, Russian, Serbian, Shona, Sindhi, Slovak, Slovenian, Somali, Southern Pashto, Spanish, Standard Latvian, Standard Malay, Swahili, Swedish, Tagalog, Tajik, Tamil, Telugu, Thai, Turkish, Ukrainian, Urdu, Vietnamese, Welsh, West Central Oromo, Western Persian, Yoruba, Zulu] in 'Source language' Dropdown component
20
+ target, # Literal[Afrikaans, Amharic, Armenian, Assamese, Basque, Belarusian, Bengali, Bosnian, Bulgarian, Burmese, Cantonese, Catalan, Cebuano, Central Kurdish, Croatian, Czech, Danish, Dutch, Egyptian Arabic, English, Estonian, Finnish, French, Galician, Ganda, Georgian, German, Greek, Gujarati, Halh Mongolian, Hebrew, Hindi, Hungarian, Icelandic, Igbo, Indonesian, Irish, Italian, Japanese, Javanese, Kannada, Kazakh, Khmer, Korean, Kyrgyz, Lao, Lithuanian, Luo, Macedonian, Maithili, Malayalam, Maltese, Mandarin Chinese, Marathi, Meitei, Modern Standard Arabic, Moroccan Arabic, Nepali, North Azerbaijani, Northern Uzbek, Norwegian Bokmål, Norwegian Nynorsk, Nyanja, Odia, Polish, Portuguese, Punjabi, Romanian, Russian, Serbian, Shona, Sindhi, Slovak, Slovenian, Somali, Southern Pashto, Spanish, Standard Latvian, Standard Malay, Swahili, Swedish, Tagalog, Tajik, Tamil, Telugu, Thai, Turkish, Ukrainian, Urdu, Vietnamese, Welsh, West Central Oromo, Western Persian, Yoruba, Zulu] in 'Target language' Dropdown component
21
+ api_name="/t2tt"
22
+ )
23
+ print(result)
24
+ return result
25
+
26
+
27
+ # Function to generate a response from the chatbot
28
+ def get_bot_response(user_input):
29
+ location = 'Benguerir, Morocco'
30
+ soil_type = 'red soil'
31
+ humidity = '40%'
32
+ weather = 'Sunny'
33
+ temp = '19C'
34
+ agriculture = 'olives'
35
+
36
+ # Add your chatbot logic here
37
+ # For simplicity, the bot echoes the user's input in this example
38
+
39
+ instruction = f'''
40
+ <s> [INST] 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
41
+ Location: {location},
42
+ land type: {soil_type}
43
+ humidity: {humidity}
44
+ weather: {weather}
45
+ temperature: {temp}
46
+ agriculture: {agriculture} [/INST]</s>
47
+ '''
48
+ prompt = f'''
49
+ 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
50
+ Location: {location},
51
+ land type: {soil_type}
52
+ humidity: {humidity}
53
+ weather: {weather}
54
+ temperature: {temp}
55
+ agriculture: {agriculture}
56
+ '''
57
+ # output = query({"inputs": f'''
58
+ # PROMPT: {prompt}
59
+ # QUESTION: {user_input}
60
+ # ANSWER:
61
+ # ''',})
62
+
63
+ output = query({"inputs": instruction, "parameters":{"max_new_tokens":250, "temperature":0.4, "return_full_text":False}})
64
+
65
+ return f"Bot: {output[0]['generated_text']}"
66
+
67
+ # Streamlit app
68
+ def main():
69
+ st.title("Simple Chatbot")
70
+
71
+ # Initialize chat history in session state
72
+ if "chat_history" not in st.session_state:
73
+ st.session_state.chat_history = []
74
+
75
+ # Text input for user to enter messages
76
+ user_input = st.text_input("You:", "")
77
+
78
+ # Button to send the message and get the bot's response
79
+ if st.button("Send"):
80
+ # Add user input to the chat history
81
+ st.session_state.chat_history.append(f"You: {user_input}")
82
+
83
+ # Get bot response and add it to the chat history
84
+ bot_response = get_bot_response(user_input)
85
+ st.session_state.chat_history.append(bot_response)
86
+
87
+ # Display the chat history using a text area
88
+ st.text_area("Chat History:", "\n".join(st.session_state.chat_history), height=200)
89
+
90
+ if st.button("Clear Chat"):
91
+ st.session_state.chat_history = []
92
+
93
+ # Run the Streamlit app
94
+ if __name__ == "__main__":
95
+ main()