CannaTech commited on
Commit
f675467
·
verified ·
1 Parent(s): a3b5a6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -136
app.py CHANGED
@@ -1,139 +1,30 @@
1
- import os
2
- import openai
3
  import gradio as gr
4
- import csv
5
- import json
6
-
7
- # Set the OpenAI API key
8
- openai.api_key = os.getenv("OPENAI_API_KEY")
9
-
10
- # Set up flagging callback function
11
- HF_TOKEN = os.getenv("HF_TOKEN")
12
- hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "CannaTech/Flagged")
13
-
14
- # Set up flagging callback function
15
- HF_TOKEN = os.getenv("HF_TOKEN")
16
- hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "CannaTech/Flagged")
17
-
18
- # Define the authentication function
19
- def check_auth(username, password):
20
- # Get the credentials from the environment variable
21
- valid_credentials_str = os.getenv("VALID_CREDENTIALS")
22
-
23
- # Parse the string into a list of tuples
24
- valid_credentials = json.loads(valid_credentials_str)
25
-
26
- # Check if the provided credentials match any valid pair
27
- for valid_username, valid_password in valid_credentials:
28
- if username == valid_username and password == valid_password:
29
- return True
30
- # If no match was found, return False
31
- return False
32
-
33
-
34
- # Initialize a list to store conversation history
35
- 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."}]
36
-
37
- # Define the function for the chatbot
38
- def CustomChatGPT(category, user_input):
39
- # Prepend category information to user input
40
- 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
41
- # Add user's message to the conversation history
42
- messages.append({"role": "user", "content": user_input})
43
- # Generate a response from the OpenAI GPT-3.5-turbo model
44
- response = openai.ChatCompletion.create(
45
- model="gpt-3.5-turbo",
46
- messages=messages
47
- )
48
- # Extract the model's message from the response
49
- ChatGPT_reply = response["choices"][0]["message"]["content"]
50
- # Add the model's message to the conversation history
51
- messages.append({"role": "assistant", "content": ChatGPT_reply})
52
- # Return the model's message
53
- return ChatGPT_reply
54
-
55
- # Define the Gradio interface
56
- iface = gr.Interface(
57
- fn=CustomChatGPT,
58
- inputs=[gr.inputs.Dropdown(choices=['BioTrack', 'Regulations', 'Best Practices', 'General Question'], label='Category'), gr.inputs.Textbox(lines=1, placeholder='Type here...', label='Your Question')],
59
- outputs=gr.outputs.Textbox(label='CannaAssist:'),
60
- show_api=False,
61
- title="CannaAssist AI Assistant",
62
- description="""Welcome to the CannaAssist AI Assistant. This tool is designed to provide expert guidance on BioTrack and cannabis regulations in New Mexico.""",
63
- examples=[
64
- ["BioTrack", "How do I update inventory quantities in BioTrack?"],
65
- ["Regulations", "What are the legal requirements for cannabis labeling in New Mexico?"],
66
- ["Best Practices", "What are the recommended storage conditions for cannabis products?"],
67
- ["General Question", "Can you explain the difference between THC and CBD?"],
68
- ["BioTrack", "How can I add a new product to my inventory in BioTrack?"],
69
- ["Regulations", "Are there restrictions on advertising cannabis products in New Mexico?"],
70
- ["Best Practices", "What are the proper sanitation procedures for cannabis cultivation facilities?"],
71
- ["General Question", "What are the benefits of using BioTrack for cannabis businesses?"],
72
- ["BioTrack", "How do I generate sales reports in BioTrack?"],
73
- ["Regulations", "What are the requirements for obtaining a cannabis license in New Mexico?"],
74
- ["Best Practices", "What are the recommended harvesting techniques for cannabis plants?"],
75
- ["General Question", "Is medical cannabis legal in New Mexico?"],
76
- ["BioTrack", "How do I track plant growth stages in BioTrack?"],
77
- ["Regulations", "What are the packaging requirements for edible cannabis products?"],
78
- ["Best Practices", "How can I optimize indoor lighting for cannabis cultivation?"],
79
- ["General Question", "What is the difference between sativa and indica cannabis strains?"],
80
- ["BioTrack", "Can BioTrack integrate with other software systems?"],
81
- ["Regulations", "Are there specific security requirements for cannabis dispensaries in New Mexico?"],
82
- ["Best Practices", "What are the recommended nutrient solutions for hydroponic cannabis cultivation?"],
83
- ["General Question", "How does cannabis consumption affect the body?"],
84
- ["BioTrack", "How do I reconcile inventory discrepancies in BioTrack?"],
85
- ["Regulations", "Are there restrictions on cannabis advertising near schools in New Mexico?"],
86
- ["Best Practices", "What are the steps for cloning cannabis plants?"],
87
- ["General Question", "What are the potential side effects of using medical cannabis?"],
88
- ["BioTrack", "How do I create purchase orders in BioTrack?"],
89
- ["Regulations", "Can I legally grow cannabis for personal use in New Mexico?"],
90
- ["Best Practices", "What are the recommended temperature and humidity levels for cannabis drying?"],
91
- ["General Question", "Is it possible to overdose on cannabis?"],
92
- ["BioTrack", "How do I handle product returns and exchanges in BioTrack?"],
93
- ["Regulations", "What are the testing requirements for cannabis products in New Mexico?"],
94
- ["Best Practices", "How often should I water my cannabis plants?"],
95
- ["General Question", "Can cannabis be used to treat specific medical conditions?"],
96
- ["BioTrack", "How do I manage multiple locations in BioTrack?"],
97
- ["Regulations", "Are there limitations on the THC potency of cannabis products in New Mexico?"],
98
- ["Best Practices", "What are the recommended pest control methods for cannabis cultivation?"],
99
- ["General Question", "What is the legal age for purchasing cannabis in New Mexico?"],
100
- ["BioTrack", "How do I track employee sales activities in BioTrack?"],
101
- ["Regulations", "Are there zoning restrictions for cannabis businesses in New Mexico?"],
102
- ["Best Practices", "What are the best pruning techniques for cannabis plants?"],
103
- ["General Question", "Can cannabis be used as a substitute for prescription medications?"],
104
- ["BioTrack", "How do I perform inventory audits in BioTrack?"],
105
- ["Regulations", "What are the transportation requirements for cannabis products in New Mexico?"],
106
- ["Best Practices", "How do I prevent and control mold in cannabis cultivation facilities?"],
107
- ["General Question", "What are the different consumption methods for cannabis?"],
108
- ["BioTrack", "How do I manage customer loyalty programs in BioTrack?"],
109
- ["Regulations", "Are there restrictions on cannabis consumption in public places in New Mexico?"],
110
- ["Best Practices", "What are the recommended techniques for cannabis plant training?"],
111
- ["General Question", "Can I travel with cannabis within New Mexico?"],
112
- ["BioTrack", "How do I handle product recalls in BioTrack?"],
113
- ["Regulations", "What are the record-keeping requirements for cannabis businesses in New Mexico?"],
114
- ], # List of example inputs
115
- theme=gr.themes.Monochrome(),
116
- examples_per_page=5,
117
- cache_examples=False, # Turn off example caching
118
- article="""Say goodbye to regulatory headaches
119
- and hello to seamless compliance with
120
- CannaAssist, our AI-powered assistant.
121
- Designed specifically for New Mexico's
122
- cannabis industry, CannaAssist leverages
123
- the power of artificial intelligence to
124
- provide personalized guidance and ensure
125
- regulatory compliance using BioTrack.
126
- From inventory management to compliance
127
- reporting, CannaAssist streamlines your
128
- operations, leaving you more time to focus
129
- on growing your business!""", # Article text
130
- thumbnail="https://assets.bigcartel.com/theme_images/101321509/IMG_6002.png", # Thumbnail image URL
131
- favicon_path="https://assets.bigcartel.com/theme_images/101321509/IMG_6002.png", # Favicon image URL
132
- allow_flagging="manual", # Enable flagging with manual control
133
- flagging_options=["Incorrect"],
134
- flagging_callback=hf_writer
135
-
136
  )
137
 
138
- # Launch the interface with authentication
139
- iface.launch(auth_message="WARNING: UNAUTHORIZED ACCESS OR USE IS STRICTLY PROHIBITED!",auth=check_auth)
 
 
 
1
  import gradio as gr
2
+ from transformers import pipeline, AutoModelForQuestionAnswering, AutoTokenizer
3
+
4
+ # Load the pre-trained model and tokenizer
5
+ model_name = "distilbert-base-uncased-distilled-squad"
6
+ model = AutoModelForQuestionAnswering.from_pretrained(model_name)
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+
9
+ # Load custom knowledge document
10
+ with open("knowledge.txt", "r") as file:
11
+ custom_knowledge = file.read()
12
+
13
+ # Initialize the question-answering pipeline
14
+ qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
15
+
16
+ def answer_question(question):
17
+ result = qa_pipeline(question=question, context=custom_knowledge)
18
+ return result['answer']
19
+
20
+ # Set up the Gradio interface
21
+ interface = gr.Interface(
22
+ fn=answer_question,
23
+ inputs="text",
24
+ outputs="text",
25
+ title="Custom Knowledge QA",
26
+ description="Ask questions based on the custom knowledge document."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  )
28
 
29
+ if __name__ == "__main__":
30
+ interface.launch()