alexspoto commited on
Commit
394bfa0
·
verified ·
1 Parent(s): a5520c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -96
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer, util
3
  import openai
4
- from openai import OpenAI
5
  import os
6
 
7
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
@@ -9,62 +8,12 @@ os.environ["TOKENIZERS_PARALLELISM"] = "false"
9
  # Initialize paths and model identifiers for easy configuration and maintenance
10
  filename = "output_topic_details.txt" # Path to the file storing chess-specific details
11
  retrieval_model_name = 'output/sentence-transformer-finetuned/'
12
-
13
- client = OpenAI(api_key="DEEPSEEK_API", base_url="https://api.deepseek.com")
14
 
15
  system_message = "You are a chatbot specialized in providing information the Young Yale Global Scholars program. You will be giving information to be applicants and scholars."
16
  # Initial system message to set the behavior of the assistant
17
  messages = [{"role": "system", "content": system_message}]
18
 
19
- # Attempt to load the necessary models and provide feedback on success or failure
20
- try:
21
- retrieval_model = SentenceTransformer(retrieval_model_name)
22
- print("Models loaded successfully.")
23
- except Exception as e:
24
- print(f"Failed to load models: {e}")
25
-
26
- def load_and_preprocess_text(filename):
27
- """
28
- Load and preprocess text from a file, removing empty lines and stripping whitespace.
29
- """
30
- try:
31
- with open(filename, 'r', encoding='utf-8') as file:
32
- segments = [line.strip() for line in file if line.strip()]
33
- print("Text loaded and preprocessed successfully.")
34
- return segments
35
- except Exception as e:
36
- print(f"Failed to load or preprocess text: {e}")
37
- return []
38
-
39
- segments = load_and_preprocess_text(filename)
40
-
41
- def find_relevant_segment(user_query, segments):
42
- """
43
- Find the most relevant text segment for a user's query using cosine similarity among sentence embeddings.
44
- This version finds the best match based on the content of the query.
45
- """
46
- try:
47
- # Lowercase the query for better matching
48
- lower_query = user_query.lower()
49
-
50
- # Encode the query and the segments
51
- query_embedding = retrieval_model.encode(lower_query)
52
- segment_embeddings = retrieval_model.encode(segments)
53
-
54
- # Compute cosine similarities between the query and the segments
55
- similarities = util.pytorch_cos_sim(query_embedding, segment_embeddings)[0]
56
-
57
- # Find the index of the most similar segment
58
- best_idx = similarities.argmax()
59
-
60
- # Return the most relevant segment
61
- return segments[best_idx]
62
- except Exception as e:
63
- print(f"Error in finding relevant segment: {e}")
64
- return ""
65
-
66
- def generate_response(user_query, relevant_segment):
67
- """
68
  Generate a response emphasizing the bot's capability in providing exercise information.
69
  """
70
  try:
@@ -72,33 +21,6 @@ def generate_response(user_query, relevant_segment):
72
 
73
  # Append user's message to messages list
74
  messages.append({"role": "user", "content": user_message})
75
-
76
- response = client.chat.completions.create(
77
- model="deepseek-chat",
78
- messages=[
79
- {"role": "system", "content": "You are a helpful assistant"},
80
- {"role": "user", "content": "Hello"},
81
- ],
82
- max_tokens=1024,
83
- temperature=0.7,
84
- stream=False
85
- )
86
-
87
-
88
- # Extract the response text
89
- output_text = response['choices'][0]['message']['content'].strip()
90
-
91
- # Append assistant's message to messages list for context
92
- messages.append({"role": "assistant", "content": output_text})
93
-
94
- return output_text
95
-
96
- except Exception as e:
97
- print(f"Error in generating response: {e}")
98
- return f"Error in generating response: {e}"
99
-
100
- def query_model(question):
101
- """
102
  Process a question, find relevant information, and generate a response.
103
  """
104
  if question == "":
@@ -106,11 +28,6 @@ def query_model(question):
106
  relevant_segment = find_relevant_segment(question, segments)
107
  if not relevant_segment:
108
  return "Could not find specific information. Please refine your question."
109
- response = generate_response(question, relevant_segment)
110
- return response
111
-
112
-
113
- # Define the welcome message and specific topics the chatbot can provide information about
114
  welcome_message = """
115
  #
116
 
@@ -131,7 +48,6 @@ topics = """
131
  """
132
 
133
 
134
-
135
  # Setup the Gradio Blocks interface with custom layout components
136
  theme = gr.themes.Monochrome(
137
  primary_hue="blue",
@@ -139,17 +55,6 @@ theme = gr.themes.Monochrome(
139
  ).set(
140
  background_fill_primary='*primary_200',
141
  background_fill_primary_dark='*primary_200',
142
- background_fill_secondary='*secondary_300',
143
- background_fill_secondary_dark='*secondary_300',
144
- border_color_accent='*secondary_200',
145
- border_color_accent_dark='*secondary_600',
146
- border_color_accent_subdued='*secondary_200',
147
- border_color_primary='*secondary_300',
148
- block_border_color='*secondary_200',
149
- button_primary_background_fill='*secondary_300',
150
- button_primary_background_fill_dark='*secondary_300',
151
- body_text_color='black')
152
-
153
  # Setup the Gradio Blocks interface with custom layout components
154
  with gr.Blocks(theme=theme) as demo:
155
  theme='gstaff/xkcd'
@@ -158,8 +63,16 @@ with gr.Blocks(theme=theme) as demo:
158
  with gr.Row():
159
  with gr.Column():
160
  gr.Markdown(topics)
 
 
161
 
162
 
 
 
 
 
 
 
163
  with gr.Row():
164
  with gr.Column():
165
  question = gr.Textbox(label="Your question", placeholder="What do you want to ask about?")
 
1
  import gradio as gr
2
  from sentence_transformers import SentenceTransformer, util
3
  import openai
 
4
  import os
5
 
6
  os.environ["TOKENIZERS_PARALLELISM"] = "false"
 
8
  # Initialize paths and model identifiers for easy configuration and maintenance
9
  filename = "output_topic_details.txt" # Path to the file storing chess-specific details
10
  retrieval_model_name = 'output/sentence-transformer-finetuned/'
11
+ openai.api_key = os.environ["OPENAI_API_KEY"]
 
12
 
13
  system_message = "You are a chatbot specialized in providing information the Young Yale Global Scholars program. You will be giving information to be applicants and scholars."
14
  # Initial system message to set the behavior of the assistant
15
  messages = [{"role": "system", "content": system_message}]
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  Generate a response emphasizing the bot's capability in providing exercise information.
18
  """
19
  try:
 
21
 
22
  # Append user's message to messages list
23
  messages.append({"role": "user", "content": user_message})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  Process a question, find relevant information, and generate a response.
25
  """
26
  if question == "":
 
28
  relevant_segment = find_relevant_segment(question, segments)
29
  if not relevant_segment:
30
  return "Could not find specific information. Please refine your question."
 
 
 
 
 
31
  welcome_message = """
32
  #
33
 
 
48
  """
49
 
50
 
 
51
  # Setup the Gradio Blocks interface with custom layout components
52
  theme = gr.themes.Monochrome(
53
  primary_hue="blue",
 
55
  ).set(
56
  background_fill_primary='*primary_200',
57
  background_fill_primary_dark='*primary_200',
 
 
 
 
 
 
 
 
 
 
 
58
  # Setup the Gradio Blocks interface with custom layout components
59
  with gr.Blocks(theme=theme) as demo:
60
  theme='gstaff/xkcd'
 
63
  with gr.Row():
64
  with gr.Column():
65
  gr.Markdown(topics)
66
+
67
+
68
 
69
 
70
+ with gr.Row():
71
+ with gr.Column():
72
+ question = gr.Textbox(label="Your question", placeholder="What do you want to ask about?")
73
+ answer = gr.Textbox(label="Ask YYGS's Response:", placeholder="askYYGS will respond here...", interactive=False, lines=10)
74
+ submit_button = gr.Button("Submit")
75
+ submit_button.click(fn=query_model, inputs=question, outputs=answer)
76
  with gr.Row():
77
  with gr.Column():
78
  question = gr.Textbox(label="Your question", placeholder="What do you want to ask about?")