CD17 commited on
Commit
21338dc
·
verified ·
1 Parent(s): c9c7941

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -173
app.py CHANGED
@@ -1,208 +1,122 @@
1
- # Configure OpenAI KEY
2
- import openai as OpenAI
3
- from dotenv import load_dotenv
4
- from openai import OpenAI
5
- import os
6
  import gradio as gr
7
-
8
- # Utility package for English Prompts
9
- import utils
10
  import json
11
  from datetime import datetime
12
 
 
 
 
13
 
14
- # Load environment variables from .env file
15
- load_dotenv()
16
-
17
- # Set your OpenAI API key from the environment variable
18
- #api_key = os.getenv("HYPERBOLIC_API_KEY") # 'ollama'
19
- #model = "meta-llama/Llama-3.2-90B-Vision-Instruct" # "gpt-4o-mini"
20
- #base_url = "https://api.hyperbolic.xyz/v1/" # ollama 'http://localhost:11434/v1/'
21
-
22
- api_key = os.getenv("OPENAI_API_KEY") # 'ollama'
23
- model = "gpt-4o-mini" # "gpt-4o-mini"
24
- base_url = None
25
-
26
-
27
- client = OpenAI(
28
- base_url=base_url,
29
- api_key=api_key
30
- )
31
-
32
-
33
- def get_completion_from_messages(messages,
34
- model="gpt-4o-mini",
35
- temperature=0,
36
- max_tokens=500):
37
- '''
38
- Encapsulate a function to access LLM
39
-
40
- Parameters:
41
- messages: This is a list of messages, each message is a dictionary containing role and content. The role can be 'system', 'user' or 'assistant', and the content is the message of the role.
42
- model: The model to be called, default is gpt-4o-mini (ChatGPT)
43
- temperature: This determines the randomness of the model output, default is 0, meaning the output will be very deterministic. Increasing temperature will make the output more random.
44
- max_tokens: This determines the maximum number of tokens in the model output.
45
- '''
46
- response = client.chat.completions.create(
47
- messages=messages,
48
- model=model,
49
- temperature=temperature, # This determines the randomness of the model's output
50
- max_tokens=max_tokens, # This determines the maximum number of tokens in the model's output
51
- )
52
-
53
- return response.choices[0].message.content
54
-
55
- def process_user_message(user_input, all_messages, debug=True):
56
  """
57
- Preprocess user messages
58
 
59
  Parameters:
60
- user_input : User input
61
- all_messages : Historical messages
62
- debug : Whether to enable DEBUG mode, enabled by default
 
 
 
63
  """
64
- # Delimiter
65
- delimiter = "```"
66
 
67
- # Step 1: Use OpenAI's Moderation API to check if the user input is compliant or an injected Prompt
68
- response = client.moderations.create(input=user_input)
69
- moderation_output = response.results[0]
70
-
71
- # The input is non-compliant after Moderation API check
72
- if moderation_output.flagged:
73
- print("Step 1: Input rejected by Moderation")
74
- return "Sorry, your request is non-compliant"
75
-
76
- # If DEBUG mode is enabled, print real-time progress
77
- if debug:
78
- print("Step 1: Input passed Moderation check")
79
- print(f"\n**user_input**: {user_input}\n\n")
80
-
81
- # Step 2: Extract products and corresponding categories
82
- category_and_product_response = utils.find_category_and_product_only(
83
- user_input, utils.get_products_and_category())
84
- #print(category_and_product_response)
85
- # Convert the extracted string to a list
86
- category_and_product_list = utils.read_string_to_list(category_and_product_response)
87
- #print(category_and_product_list)
88
-
89
- if debug: print("Step 2: Extracted product list")
90
-
91
- # Step 3: Find corresponding product information
92
- product_information = utils.generate_output_string(category_and_product_list)
93
- if debug:
94
- print("Step 3: Found information for extracted products")
95
- print(f"\n**product_information**: {product_information}\n\n")
96
 
97
- # Step 4: Generate answer based on information
98
- system_message = f"""
99
- You are a customer service assistant for a large electronic store. \
100
- Respond in a friendly and helpful tone, with concise answers. \
101
- Make sure to ask the user relevant follow-up questions.
102
  """
103
- # Insert message
104
- messages = [
105
- {'role': 'system', 'content': system_message},
106
- {'role': 'user', 'content': f"{delimiter}{user_input}{delimiter}"},
107
- {'role': 'assistant', 'content': f"Relevant product information:\n{product_information}"}
108
- ]
109
- # Get GPT3.5's answer
110
- # Implement multi-turn dialogue by appending all_messages
111
- final_response = get_completion_from_messages(all_messages + messages)
112
- if debug:print("Step 4: Generated user answer")
113
- # Add this round of information to historical messages
114
- all_messages = all_messages + messages[1:]
115
-
116
- # Step 5: Check if the output is compliant based on Moderation API
117
- response = client.moderations.create(input=final_response)
118
- moderation_output = response.results[0]
119
-
120
- # Output is non-compliant
121
- if moderation_output.flagged:
122
- if debug: print("Step 5: Output rejected by Moderation")
123
- return "Sorry, we cannot provide that information"
124
-
125
- if debug: print("Step 5: Output passed Moderation check")
126
-
127
- # Step 6: Model checks if the user's question is well answered
128
- user_message = f"""
129
- Customer message: {delimiter}{user_input}{delimiter}
130
- Agent response: {delimiter}{final_response}{delimiter}
131
-
132
- Does the response sufficiently answer the question? answer Yes or No
133
  """
134
- messages = [
135
- {'role': 'system', 'content': system_message},
136
- {'role': 'user', 'content': user_message}
137
- ]
138
- # Request model to evaluate the answer
139
- evaluation_response = get_completion_from_messages(messages)
140
- if debug: print("Step 6: Model evaluated the answer")
141
-
142
- # Step 7: If evaluated as Y, output the answer; if evaluated as N, feedback that the answer will be manually corrected
143
- if "Y" in evaluation_response: # Use 'in' to avoid the model possibly generating Yes
144
- if debug: print("Step 7: Model approved the answer.")
145
- return final_response, all_messages, category_and_product_response
146
- else:
147
- if debug: print("Step 7: Model disapproved the answer.")
148
- neg_str = "I apologize, but I cannot provide the information you need. I will transfer you to a human customer service representative for further assistance."
149
- return neg_str, all_messages
150
-
151
- #Visual Interface
152
- #log messages
153
- messages_log = 'messages_log.json'
154
-
155
- def log_messages(new_element, filepath):
156
- # Update the messages_log with the new assistant response
157
- filepath = 'messages_log.json'
 
158
 
 
 
159
  try:
160
  with open(filepath, "r") as file:
161
- # Check if the file is empty
162
  if file.read().strip() == "":
163
- data = [] # Initialize with an empty list or dictionary as needed
164
  else:
165
- file.seek(0) # Move the cursor back to the start of the file
166
  data = json.load(file)
167
- except FileNotFoundError:
168
- # If the file doesn't exist, start with an empty list or dictionary
169
- data = []
170
- except json.JSONDecodeError:
171
- # If there is a JSON decoding error, handle it by initializing empty data
172
- print("Error: The JSON file is not properly formatted.")
173
  data = []
174
 
175
- # Assuming the data is a list of items
176
  data.append(new_element)
177
-
178
  with open(filepath, "w") as file:
179
  json.dump(data, file, indent=4)
180
 
181
- # Initialize the context as an empty list to keep track of the conversation history
182
  context = []
183
 
184
  # Function to collect and process user messages
185
  def collect_messages_en(input_text, debug=True):
186
- global context # Use the global messages_log to track all messages
187
 
188
- if debug: print(f"User Input = {input_text}")
 
189
  if input_text == "":
190
  return
191
- #context = get_messages()
192
- # Process the user input and get a response
193
- response, context, product_category = process_user_message(input_text, context, debug=debug)
194
- context.append({'role':'assistant', 'content':f"{response}"})
195
-
196
  # Get the current timestamp
197
- current_timestamp = datetime.now()
198
- formatted_timestamp = current_timestamp.strftime("%Y-%m-%d %H:%M:%S")
199
-
200
- #log the messages
201
- log_messages({'time_stamp': formatted_timestamp, 'user_input': input_text, 'AI_response': response, 'metadata': product_category}, messages_log)
202
- # Return the response to be displayed in the Gradio interface
203
  return response
204
 
205
- # Create a Gradio interface for interacting with the assistant
206
  demo = gr.Interface(
207
  fn=collect_messages_en,
208
  inputs=gr.Textbox(lines=3, label="Inquiries", placeholder="Ask us anything..."),
@@ -212,6 +126,3 @@ demo = gr.Interface(
212
  )
213
 
214
  demo.launch()
215
-
216
- # user_input = "tell me about the smartx pro phone and the fotosnap camera, the dslr one. Also what tell me about your tvs"
217
-
 
1
+ # Import necessary libraries
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
 
 
 
3
  import gradio as gr
 
 
 
4
  import json
5
  from datetime import datetime
6
 
7
+ # Load the GPT-2 model and tokenizer from Hugging Face
8
+ tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
9
+ model = AutoModelForCausalLM.from_pretrained("openai-community/gpt2")
10
 
11
+ # Utility function for generating responses using the GPT-2 model
12
+ def generate_response(messages, max_tokens=500, temperature=0.7):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  """
14
+ Generate a response from the model based on the input messages.
15
 
16
  Parameters:
17
+ - messages: List of dictionaries containing the role and content of each message.
18
+ - max_tokens: Maximum number of tokens to generate.
19
+ - temperature: Controls randomness in the output.
20
+
21
+ Returns:
22
+ - The generated response as a string.
23
  """
24
+ # Concatenate messages into a single prompt
25
+ prompt = "\n".join([f"{msg['role']}: {msg['content']}" for msg in messages])
26
 
27
+ # Tokenize the input prompt
28
+ input_ids = tokenizer.encode(prompt, return_tensors='pt')
29
+
30
+ # Generate response
31
+ output = model.generate(input_ids, max_length=len(input_ids[0]) + max_tokens,
32
+ temperature=temperature, pad_token_id=tokenizer.eos_token_id)
33
+
34
+ # Decode the output to a string
35
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
36
+
37
+ # Return the generated response, excluding the input prompt for clarity
38
+ return response[len(prompt):].strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
+ # Function to process user input and generate a response
41
+ def process_user_message(user_input, all_messages, debug=True):
 
 
 
42
  """
43
+ Process the user message and generate a response.
44
+
45
+ Parameters:
46
+ - user_input: The input from the user.
47
+ - all_messages: A list of previous messages in the conversation.
48
+ - debug: Whether to enable debug logging.
49
+
50
+ Returns:
51
+ - The response from the model and the updated message history.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  """
53
+ # Add the user's message to the conversation history
54
+ all_messages.append({'role': 'user', 'content': user_input})
55
+
56
+ # Define a system message for context
57
+ system_message = {
58
+ 'role': 'system',
59
+ 'content': "You are a helpful assistant. Answer the user's question as accurately as possible."
60
+ }
61
+
62
+ # Include the system message and conversation history
63
+ messages = [system_message] + all_messages
64
+
65
+ # Generate a response using the model
66
+ response = generate_response(messages, max_tokens=500, temperature=0.7)
67
+
68
+ # Add the model's response to the conversation history
69
+ all_messages.append({'role': 'assistant', 'content': response})
70
+
71
+ # If debug is enabled, print the conversation history
72
+ if debug:
73
+ print("Conversation History:")
74
+ for msg in all_messages:
75
+ print(f"{msg['role']}: {msg['content']}")
76
+
77
+ return response, all_messages
78
 
79
+ # Function to log the messages to a JSON file
80
+ def log_messages(new_element, filepath='messages_log.json'):
81
  try:
82
  with open(filepath, "r") as file:
 
83
  if file.read().strip() == "":
84
+ data = []
85
  else:
86
+ file.seek(0)
87
  data = json.load(file)
88
+ except (FileNotFoundError, json.JSONDecodeError):
 
 
 
 
 
89
  data = []
90
 
 
91
  data.append(new_element)
92
+
93
  with open(filepath, "w") as file:
94
  json.dump(data, file, indent=4)
95
 
96
+ # Initialize an empty list to keep track of the conversation history
97
  context = []
98
 
99
  # Function to collect and process user messages
100
  def collect_messages_en(input_text, debug=True):
101
+ global context
102
 
103
+ if debug:
104
+ print(f"User Input: {input_text}")
105
  if input_text == "":
106
  return
107
+
108
+ # Process the user input and generate a response
109
+ response, context = process_user_message(input_text, context, debug=debug)
110
+
 
111
  # Get the current timestamp
112
+ current_timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
113
+
114
+ # Log the messages
115
+ log_messages({'time_stamp': current_timestamp, 'user_input': input_text, 'AI_response': response})
116
+
 
117
  return response
118
 
119
+ # Create a Gradio interface for the assistant
120
  demo = gr.Interface(
121
  fn=collect_messages_en,
122
  inputs=gr.Textbox(lines=3, label="Inquiries", placeholder="Ask us anything..."),
 
126
  )
127
 
128
  demo.launch()