Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
import requests | |
from gradio_client import Client | |
API_TOKEN = st.secrets['HF_TOKEN'] | |
API_URL = "https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1" | |
headers = {"Authorization": f"Bearer {str(API_TOKEN)}"} | |
def query(payload): | |
response = requests.post(API_URL, headers=headers, json=payload) | |
return response.json() | |
def translate(text,source="English",target="Moroccan Arabic"): | |
client = Client("https://facebook-seamless-m4t-v2-large.hf.space/--replicas/6w5sk/") | |
result = client.predict( | |
text, # str in 'Input text' Textbox component | |
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 | |
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 | |
api_name="/t2tt" | |
) | |
print(result) | |
return result | |
# Function to generate a response from the chatbot | |
def get_bot_response(user_input): | |
location = 'Benguerir, Morocco' | |
soil_type = 'red soil' | |
humidity = '40%' | |
weather = 'Sunny' | |
temp = '19C' | |
agriculture = 'olives' | |
# Add your chatbot logic here | |
# For simplicity, the bot echoes the user's input in this example | |
instruction = f''' | |
<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 | |
Location: {location}, | |
land type: {soil_type} | |
humidity: {humidity} | |
weather: {weather} | |
temperature: {temp} | |
agriculture: {agriculture} [/INST]</s> | |
''' | |
prompt = f''' | |
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 | |
Location: {location}, | |
land type: {soil_type} | |
humidity: {humidity} | |
weather: {weather} | |
temperature: {temp} | |
agriculture: {agriculture} | |
''' | |
# output = query({"inputs": f''' | |
# PROMPT: {prompt} | |
# QUESTION: {user_input} | |
# ANSWER: | |
# ''',}) | |
output = query({"inputs": instruction, "parameters":{"max_new_tokens":250, "temperature":0.4, "return_full_text":False}}) | |
print(headers) | |
print(output) | |
return f"Bot: {translate(output[0]['generated_text'])}" | |
# Streamlit app | |
def main(): | |
st.title("Simple Chatbot") | |
# Initialize chat history in session state | |
if "chat_history" not in st.session_state: | |
st.session_state.chat_history = [] | |
# Text input for user to enter messages | |
user_input = st.text_input("You:", "") | |
# Button to send the message and get the bot's response | |
if st.button("Send"): | |
# Add user input to the chat history | |
st.session_state.chat_history.append(f"You: {user_input}") | |
# Get bot response and add it to the chat history | |
bot_response = get_bot_response(user_input) | |
st.session_state.chat_history.append(bot_response) | |
# Display the chat history using a text area | |
st.text_area("Chat History:", "\n".join(st.session_state.chat_history), height=200) | |
if st.button("Clear Chat"): | |
st.session_state.chat_history = [] | |
# Run the Streamlit app | |
if __name__ == "__main__": | |
main() | |