import os import openai import gradio as gr import csv import json # Set the OpenAI API key openai.api_key = os.getenv("OPENAI_API_KEY") # Set up flagging callback function HF_TOKEN = os.getenv("HF_TOKEN") hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "CannaTech/Flagged") # Set up flagging callback function HF_TOKEN = os.getenv("HF_TOKEN") hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "CannaTech/Flagged") # Define the authentication function def check_auth(username, password): # Get the credentials from the environment variable valid_credentials_str = os.getenv("VALID_CREDENTIALS") # Parse the string into a list of tuples valid_credentials = json.loads(valid_credentials_str) # Check if the provided credentials match any valid pair for valid_username, valid_password in valid_credentials: if username == valid_username and password == valid_password: return True # If no match was found, return False return False # Initialize a list to store conversation history messages = [{"role": "system", "content": "Assume you are an expert in the recreational cannabis market in New Mexico with robust experience in the state’s regulatory environment and knowledge of the laws that govern the recreational cannabis industry both in the state and federally in the United States. You specialize in a piece of software called BioTrack, which is state mandated track-and-trace compliance software that allows seed-to-sale tracking of wholesale bulk cannabis flower, manufactured cannabis products, and also includes inventory management and point of sale tools for retailers."}] # Define the function for the chatbot def CustomChatGPT(category, user_input): # Prepend category information to user input user_input = f"Anytime you are asked to respond to a query you are to assume that you are speaking as an advisor using your expertise to consult a licensed cannabis business owner and that all discussion concerning cannabis is in the context of {category} and concerns legal recreational cannabis in New Mexico. Use your knowledge of the subjects in which you specialize and your experience to provide the best technical support and customer service possible." + user_input # Add user's message to the conversation history messages.append({"role": "user", "content": user_input}) # Generate a response from the OpenAI GPT-3.5-turbo model response = openai.ChatCompletion.create( model="gpt-3.5-turbo", messages=messages ) # Extract the model's message from the response ChatGPT_reply = response["choices"][0]["message"]["content"] # Add the model's message to the conversation history messages.append({"role": "assistant", "content": ChatGPT_reply}) # Return the model's message return ChatGPT_reply # Define the Gradio interface iface = gr.Interface( fn=CustomChatGPT, inputs=[gr.inputs.Dropdown(choices=['BioTrack', 'Regulations', 'Best Practices', 'General Question'], label='Category'), gr.inputs.Textbox(lines=1, placeholder='Type here...', label='Your Question')], outputs=gr.outputs.Textbox(label='CannaAssist:'), show_api=False, title="CannaAssist AI Assistant", description="""Welcome to the CannaAssist AI Assistant. This tool is designed to provide expert guidance on BioTrack and cannabis regulations in New Mexico.""", examples=[ ["BioTrack", "How do I update inventory quantities in BioTrack?"], ["Regulations", "What are the legal requirements for cannabis labeling in New Mexico?"], ["Best Practices", "What are the recommended storage conditions for cannabis products?"], ["General Question", "Can you explain the difference between THC and CBD?"], ["BioTrack", "How can I add a new product to my inventory in BioTrack?"], ["Regulations", "Are there restrictions on advertising cannabis products in New Mexico?"], ["Best Practices", "What are the proper sanitation procedures for cannabis cultivation facilities?"], ["General Question", "What are the benefits of using BioTrack for cannabis businesses?"], ["BioTrack", "How do I generate sales reports in BioTrack?"], ["Regulations", "What are the requirements for obtaining a cannabis license in New Mexico?"], ["Best Practices", "What are the recommended harvesting techniques for cannabis plants?"], ["General Question", "Is medical cannabis legal in New Mexico?"], ["BioTrack", "How do I track plant growth stages in BioTrack?"], ["Regulations", "What are the packaging requirements for edible cannabis products?"], ["Best Practices", "How can I optimize indoor lighting for cannabis cultivation?"], ["General Question", "What is the difference between sativa and indica cannabis strains?"], ["BioTrack", "Can BioTrack integrate with other software systems?"], ["Regulations", "Are there specific security requirements for cannabis dispensaries in New Mexico?"], ["Best Practices", "What are the recommended nutrient solutions for hydroponic cannabis cultivation?"], ["General Question", "How does cannabis consumption affect the body?"], ["BioTrack", "How do I reconcile inventory discrepancies in BioTrack?"], ["Regulations", "Are there restrictions on cannabis advertising near schools in New Mexico?"], ["Best Practices", "What are the steps for cloning cannabis plants?"], ["General Question", "What are the potential side effects of using medical cannabis?"], ["BioTrack", "How do I create purchase orders in BioTrack?"], ["Regulations", "Can I legally grow cannabis for personal use in New Mexico?"], ["Best Practices", "What are the recommended temperature and humidity levels for cannabis drying?"], ["General Question", "Is it possible to overdose on cannabis?"], ["BioTrack", "How do I handle product returns and exchanges in BioTrack?"], ["Regulations", "What are the testing requirements for cannabis products in New Mexico?"], ["Best Practices", "How often should I water my cannabis plants?"], ["General Question", "Can cannabis be used to treat specific medical conditions?"], ["BioTrack", "How do I manage multiple locations in BioTrack?"], ["Regulations", "Are there limitations on the THC potency of cannabis products in New Mexico?"], ["Best Practices", "What are the recommended pest control methods for cannabis cultivation?"], ["General Question", "What is the legal age for purchasing cannabis in New Mexico?"], ["BioTrack", "How do I track employee sales activities in BioTrack?"], ["Regulations", "Are there zoning restrictions for cannabis businesses in New Mexico?"], ["Best Practices", "What are the best pruning techniques for cannabis plants?"], ["General Question", "Can cannabis be used as a substitute for prescription medications?"], ["BioTrack", "How do I perform inventory audits in BioTrack?"], ["Regulations", "What are the transportation requirements for cannabis products in New Mexico?"], ["Best Practices", "How do I prevent and control mold in cannabis cultivation facilities?"], ["General Question", "What are the different consumption methods for cannabis?"], ["BioTrack", "How do I manage customer loyalty programs in BioTrack?"], ["Regulations", "Are there restrictions on cannabis consumption in public places in New Mexico?"], ["Best Practices", "What are the recommended techniques for cannabis plant training?"], ["General Question", "Can I travel with cannabis within New Mexico?"], ["BioTrack", "How do I handle product recalls in BioTrack?"], ["Regulations", "What are the record-keeping requirements for cannabis businesses in New Mexico?"], ], # List of example inputs theme=gr.themes.Monochrome(), examples_per_page=5, cache_examples=False, # Turn off example caching article="""Say goodbye to regulatory headaches and hello to seamless compliance with CannaAssist, our AI-powered assistant. Designed specifically for New Mexico's cannabis industry, CannaAssist leverages the power of artificial intelligence to provide personalized guidance and ensure regulatory compliance using BioTrack. From inventory management to compliance reporting, CannaAssist streamlines your operations, leaving you more time to focus on growing your business!""", # Article text thumbnail="https://assets.bigcartel.com/theme_images/101321509/IMG_6002.png", # Thumbnail image URL favicon_path="https://assets.bigcartel.com/theme_images/101321509/IMG_6002.png", # Favicon image URL allow_flagging="manual", # Enable flagging with manual control flagging_options=["Incorrect"], flagging_callback=hf_writer ) # Launch the interface with authentication iface.launch(auth_message="WARNING: UNAUTHORIZED ACCESS OR USE IS STRICTLY PROHIBITED!",auth=check_auth)