allyyy commited on
Commit
2af0c05
·
verified ·
1 Parent(s): 99ecc79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -15
app.py CHANGED
@@ -4,31 +4,31 @@ from transformers import GPT2LMHeadModel, GPT2Tokenizer
4
  import os
5
  import torch
6
 
7
- os.environ["TOKENIZERS_PARALLELISM"] = "false"
 
8
 
9
- # Initialize paths and model identifiers for easy configuration and maintenance
10
- filename = "output_topic_details.txt" # Path to the file storing destress-specific details
11
- retrieval_model_name = 'output/sentence-transformer-finetuned/'
12
 
13
- # Load GPT-2 model and tokenizer
14
- tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
15
- model = GPT2LMHeadModel.from_pretrained("gpt2")
16
 
17
  system_message = "You are a comfort chatbot specialized in providing information on therapy, destressing activities, and student opportunities."
18
  messages = [{"role": "system", "content": system_message}]
19
  messages.append({
20
  "role": "system",
21
- "content": "Do not use Markdown Format. Do not include hashtags or asterisks"
22
  })
23
 
24
- # Load the retrieval model
25
- try:
26
- retrieval_model = SentenceTransformer(retrieval_model_name)
27
- print("Models loaded successfully.")
28
- except Exception as e:
29
- print(f"Failed to load models: {e}")
30
-
31
  def load_and_preprocess_text(filename):
 
 
 
 
 
 
 
 
 
32
  try:
33
  with open(filename, 'r', encoding='utf-8') as file:
34
  segments = [line.strip() for line in file if line.strip()]
@@ -40,6 +40,31 @@ def load_and_preprocess_text(filename):
40
 
41
  segments = load_and_preprocess_text(filename)
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  def find_relevant_segment(user_query, segments):
44
  try:
45
  lower_query = user_query.lower()
 
4
  import os
5
  import torch
6
 
7
+ import os
8
+ import openai
9
 
10
+ # Ensure the OpenAI API key is set
11
+ openai.api_key = os.getenv("OPENAI_API_KEY")
 
12
 
13
+ filename = "output_topic_details.txt" # Path to the file storing destress-specific details
 
 
14
 
15
  system_message = "You are a comfort chatbot specialized in providing information on therapy, destressing activities, and student opportunities."
16
  messages = [{"role": "system", "content": system_message}]
17
  messages.append({
18
  "role": "system",
19
+ "content": "Do not use Markdown Format. Do not include hashtags or asterisks."
20
  })
21
 
 
 
 
 
 
 
 
22
  def load_and_preprocess_text(filename):
23
+ """
24
+ Load and preprocess text data from a file.
25
+
26
+ Args:
27
+ - filename (str): Path to the text file to be loaded.
28
+
29
+ Returns:
30
+ - list[str]: List of preprocessed text segments.
31
+ """
32
  try:
33
  with open(filename, 'r', encoding='utf-8') as file:
34
  segments = [line.strip() for line in file if line.strip()]
 
40
 
41
  segments = load_and_preprocess_text(filename)
42
 
43
+ def generate_response_gpt4(user_query, relevant_segment):
44
+ try:
45
+ # Construct the messages for the chat history
46
+ messages.append({"role": "user", "content": user_query})
47
+ messages.append({"role": "assistant", "content": relevant_segment})
48
+
49
+ # Use OpenAI's API to generate a response
50
+ response = openai.ChatCompletion.create(
51
+ model="gpt-4",
52
+ messages=messages,
53
+ max_tokens=150, # Control the length of the response
54
+ temperature=0.7 # Adjust creativity as needed
55
+ )
56
+
57
+ # Get the assistant's response from the generated content
58
+ assistant_reply = response['choices'][0]['message']['content']
59
+
60
+ # Append the assistant's response to the conversation history
61
+ messages.append({"role": "assistant", "content": assistant_reply})
62
+ return assistant_reply
63
+
64
+ except Exception as e:
65
+ print(f"Error in generating response: {e}")
66
+ return f"Error in generating response: {e}"
67
+
68
  def find_relevant_segment(user_query, segments):
69
  try:
70
  lower_query = user_query.lower()