LuckyHappyFish commited on
Commit
3a8e74d
·
1 Parent(s): c397a05
Files changed (1) hide show
  1. app.py +29 -58
app.py CHANGED
@@ -2,14 +2,11 @@ import gradio as gr
2
  from huggingface_hub import InferenceClient
3
  import re
4
  import traceback
 
5
 
6
  # Initialize the Inference Client with your model
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
- # Regular expression to validate YouTube URLs (modify if needed)
10
- YOUTUBE_URL_PATTERN = re.compile(
11
- r'(https?://)?(www\.)?(youtube\.com|youtu\.?be)/.+'
12
- )
13
 
14
  def respond(
15
  message,
@@ -42,41 +39,34 @@ def respond(
42
  # Append the latest user message
43
  messages.append({"role": "user", "content": message})
44
 
45
- response = ""
46
- success = False # Flag to determine if negotiation was successful
47
-
48
  try:
49
- # Generate the AI response
50
- for msg in client.chat_completion(
51
  messages,
52
  max_tokens=max_tokens,
53
- stream=True,
54
  temperature=temperature,
55
  top_p=top_p,
56
- ):
57
- token = msg.choices[0].delta.content
58
- response += token
59
- yield response
60
-
61
- # Simple logic to determine negotiation success based on AI's response
62
- success_keywords = ["agreed", "accepted", "increase", "raised", "enhanced", "approved", "offer"]
63
-
64
- # Check if any success keywords are in the AI's response (case-insensitive)
65
- if any(keyword.lower() in response.lower() for keyword in success_keywords):
66
- # Increment the salary by a fixed amount
67
- increment = 5000 # Fixed increment; modify as desired
68
- current_salary += increment
69
- success = True
70
- yield f"\n\n🎉 Negotiation successful! Your salary has been increased to ${current_salary:,}."
71
-
72
  except Exception as e:
73
  response = f"An error occurred while communicating with the AI: {e}"
74
- yield response
75
  traceback.print_exc()
76
 
77
- # Update the salary display if negotiation was successful
78
- if success:
79
- yield gr.update(value=current_salary)
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  def reset_game():
82
  """
@@ -114,7 +104,7 @@ with gr.Blocks() as demo:
114
  with gr.Column(scale=3):
115
  # Chat history to keep track of the conversation
116
  chat_history = gr.State([])
117
-
118
  # Chat Interface
119
  chatbot = gr.Chatbot()
120
 
@@ -125,33 +115,13 @@ with gr.Blocks() as demo:
125
  lines=2
126
  )
127
  send_button = gr.Button("Send")
128
-
129
  def handle_message(message, history, max_tokens, temperature, top_p, current_salary):
130
  """
131
  Handles user messages and updates the conversation history and salary.
132
  """
133
- generator = respond(message, history, max_tokens, temperature, top_p, current_salary)
134
- try:
135
- # Get the initial AI response
136
- response = next(generator)
137
- # Update history
138
- history.append((message, response))
139
- # Process the rest of the generator
140
- try:
141
- while True:
142
- additional_response = next(generator)
143
- response += additional_response
144
- # Check for negotiation success message
145
- if "Negotiation successful" in additional_response:
146
- current_salary = int(re.search(r'\$(\d{1,3}(?:,\d{3})*)', additional_response).group(1).replace(',', ''))
147
- yield (history, current_salary)
148
- except StopIteration:
149
- pass
150
- except StopIteration:
151
- pass
152
- except Exception as e:
153
- history.append((message, f"An error occurred: {e}"))
154
- yield (history, current_salary)
155
 
156
  send_button.click(
157
  handle_message,
@@ -171,10 +141,11 @@ with gr.Blocks() as demo:
171
  ],
172
  outputs=[
173
  chatbot,
174
- salary_display
 
175
  ]
176
  )
177
-
178
  gr.Markdown(
179
  """
180
  ---
 
2
  from huggingface_hub import InferenceClient
3
  import re
4
  import traceback
5
+ import os
6
 
7
  # Initialize the Inference Client with your model
8
+ # Ensure you have set the HUGGINGFACE_API_TOKEN environment variable
9
+ client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=os.getenv("HUGGINGFACE_API_TOKEN"))
 
 
 
 
10
 
11
  def respond(
12
  message,
 
39
  # Append the latest user message
40
  messages.append({"role": "user", "content": message})
41
 
42
+ # Generate the AI response
 
 
43
  try:
44
+ response = client.chat_completion(
 
45
  messages,
46
  max_tokens=max_tokens,
 
47
  temperature=temperature,
48
  top_p=top_p,
49
+ stop=None
50
+ ).get("choices")[0].get("message").get("content")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  except Exception as e:
52
  response = f"An error occurred while communicating with the AI: {e}"
 
53
  traceback.print_exc()
54
 
55
+ # Append AI response to history
56
+ history.append((message, response))
57
+
58
+ # Simple logic to determine negotiation success based on AI's response
59
+ success_keywords = ["agreed", "accepted", "increase", "raised", "enhanced", "approved", "offer", "agree", "accept"]
60
+
61
+ # Check if any success keywords are in the AI's response (case-insensitive)
62
+ if any(keyword.lower() in response.lower() for keyword in success_keywords):
63
+ # Increment the salary by a fixed amount
64
+ increment = 5000 # Fixed increment; modify as desired
65
+ current_salary += increment
66
+ negotiation_result = f"🎉 Negotiation successful! Your salary has been increased to ${current_salary:,}."
67
+ history.append(("", negotiation_result))
68
+
69
+ return history, current_salary
70
 
71
  def reset_game():
72
  """
 
104
  with gr.Column(scale=3):
105
  # Chat history to keep track of the conversation
106
  chat_history = gr.State([])
107
+
108
  # Chat Interface
109
  chatbot = gr.Chatbot()
110
 
 
115
  lines=2
116
  )
117
  send_button = gr.Button("Send")
118
+
119
  def handle_message(message, history, max_tokens, temperature, top_p, current_salary):
120
  """
121
  Handles user messages and updates the conversation history and salary.
122
  """
123
+ history, current_salary = respond(message, history, max_tokens, temperature, top_p, current_salary)
124
+ return history, current_salary, "" # Clear the input textbox
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
 
126
  send_button.click(
127
  handle_message,
 
141
  ],
142
  outputs=[
143
  chatbot,
144
+ salary_display,
145
+ user_input # Clear the input textbox
146
  ]
147
  )
148
+
149
  gr.Markdown(
150
  """
151
  ---