vericudebuget commited on
Commit
fa11edf
1 Parent(s): 58c68ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -52
app.py CHANGED
@@ -2,22 +2,15 @@ from huggingface_hub import InferenceClient
2
  import gradio as gr
3
  import datetime
4
  import re
5
- import requests
6
- import json
7
 
8
  # Initialize the InferenceClient
9
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
10
 
11
- # Enter your API key here
12
- api_key = "1e8cd0385845a649e448cde4917058d6"
13
-
14
  # Define the system prompt templates
15
  system_prompt_templates = {
16
- r"\btime\b|\bhour\b|\bclock\b": "server log: ~This message was sent at {formatted_time}. The actual year is 2024.~",
17
- r"\bweather\b|\bforecast\b|\bmeteo": "server log: ~The current weather conditions in {city_name} are {weather_description} with a high of {current_temperature_c}°C ({current_temperature_f}°F) and a pressure of {current_pressure_hpa} hPa ({current_pressure_inHg} inHg) and humidity of {current_humidity}%.~",
18
  r"\bdate\b|\bcalendar\b": "server log: ~Today's date is {formatted_date}.~",
19
- r"\bpolitics\b|\belection\b": "server log: ~This conversation is taking place in a politically neutral environment.~"
20
- }
21
 
22
  def format_prompt(message, history, system_prompt):
23
  prompt = "<s>"
@@ -45,40 +38,9 @@ def generate(prompt, history, system_prompt, temperature=0.9, max_new_tokens=904
45
  formatted_date = now.strftime("%B %d, %Y")
46
 
47
  # Check for keywords in the user's input and update the system prompt accordingly
48
- city_name = None
49
- weather_description = None
50
- current_temperature_c = None
51
- current_temperature_f = None
52
- current_pressure_hpa = None
53
- current_pressure_inHg = None
54
- current_humidity = None
55
  for keyword, template in system_prompt_templates.items():
56
  if re.search(keyword, prompt, re.IGNORECASE):
57
- if keyword == r"\bweather\b|\bforecast\b|\bmeteo":
58
- base_url = "http://api.openweathermap.org/data/2.5/weather?"
59
- complete_url = base_url + "appid=" + api_key + "&q=" + city_name
60
- response = requests.get(complete_url)
61
- x = response.json()
62
- if x["cod"] != "404":
63
- y = x["main"]
64
- current_temperature_c = y["temp"] - 273.15 # Convert from Kelvin to Celsius
65
- current_temperature_f = current_temperature_c * 9/5 + 32 # Convert from Celsius to Fahrenheit
66
- current_pressure_hpa = y["pressure"]
67
- current_pressure_inHg = current_pressure_hpa * 0.02953 # Convert from hPa to inHg
68
- current_humidity = y["humidity"]
69
- z = x["weather"]
70
- weather_description = z[0]["description"]
71
- city_name = x["name"]
72
- else:
73
- print("City Not Found")
74
- city_name = "unknown"
75
- weather_description = "unknown"
76
- current_temperature_c = 0
77
- current_temperature_f = 32
78
- current_pressure_hpa = 0
79
- current_pressure_inHg = 0
80
- current_humidity = 0
81
- system_prompt = template.format(formatted_time=formatted_time, formatted_date=formatted_date, city_name=city_name, weather_description=weather_description, current_temperature_c=current_temperature_c, current_temperature_f=current_temperature_f, current_pressure_hpa=current_pressure_hpa, current_pressure_inHg=current_pressure_inHg, current_humidity=current_humidity)
82
  break
83
 
84
  formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
@@ -88,26 +50,37 @@ def generate(prompt, history, system_prompt, temperature=0.9, max_new_tokens=904
88
  output += response.token.text
89
  yield output
90
 
91
- with gr.Blocks() as demo:
92
- chatbot = gr.Chatbot(show_label=True, show_share_button=False, show_copy_button=True, likeable=True, layout="panel")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93
  with gr.Row():
94
  with gr.Column(scale=3):
95
  user_input = gr.Textbox(label="Your message", placeholder="Type your message here...")
96
  with gr.Column(scale=1):
97
  submit_button = gr.Button("Send")
98
 
99
- def check_keywords(text):
100
- for keyword, _ in system_prompt_templates.items():
101
- if re.search(keyword, text, re.IGNORECASE):
102
- return True
103
- return False
104
 
105
- user_input.submit(
106
  fn=generate,
107
  inputs=[user_input, chatbot, gr.Textbox(label="System Prompt", max_lines=1, interactive=True)],
108
- outputs=chatbot,
109
  every=200,
110
- _js=None
111
  )
112
 
113
- demo.launch(show_api=False)
 
2
  import gradio as gr
3
  import datetime
4
  import re
 
 
5
 
6
  # Initialize the InferenceClient
7
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
8
 
 
 
 
9
  # Define the system prompt templates
10
  system_prompt_templates = {
11
+ r"\btime\b|\bhour\b|\bclock\b": "server log: ~This message was sent at {formatted_time}.~",
 
12
  r"\bdate\b|\bcalendar\b": "server log: ~Today's date is {formatted_date}.~",
13
+
 
14
 
15
  def format_prompt(message, history, system_prompt):
16
  prompt = "<s>"
 
38
  formatted_date = now.strftime("%B %d, %Y")
39
 
40
  # Check for keywords in the user's input and update the system prompt accordingly
 
 
 
 
 
 
 
41
  for keyword, template in system_prompt_templates.items():
42
  if re.search(keyword, prompt, re.IGNORECASE):
43
+ system_prompt = template.format(formatted_time=formatted_time, formatted_date=formatted_date)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  break
45
 
46
  formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
 
50
  output += response.token.text
51
  yield output
52
 
53
+ additional_inputs = [
54
+ gr.Textbox(label="System Prompt", max_lines=1, interactive=True),
55
+ gr.Slider(label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs"),
56
+ gr.Slider(label="Max new tokens", value=9048, minimum=256, maximum=9048, step=64, interactive=True, info="The maximum numbers of new tokens"),
57
+ gr.Slider(label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens"),
58
+ gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens")
59
+ ]
60
+
61
+ def check_keywords(text):
62
+ for keyword, _ in system_prompt_templates.items():
63
+ if re.search(keyword, text, re.IGNORECASE):
64
+ return True
65
+ return False
66
+
67
+ chatbot = gr.Chatbot(show_label=True, show_share_button=False, show_copy_button=True, likeable=True, layout="panel")
68
+ with gr.Blocks():
69
  with gr.Row():
70
  with gr.Column(scale=3):
71
  user_input = gr.Textbox(label="Your message", placeholder="Type your message here...")
72
  with gr.Column(scale=1):
73
  submit_button = gr.Button("Send")
74
 
75
+ with gr.Row():
76
+ chatbot_output = chatbot
 
 
 
77
 
78
+ submit_button.click(
79
  fn=generate,
80
  inputs=[user_input, chatbot, gr.Textbox(label="System Prompt", max_lines=1, interactive=True)],
81
+ outputs=chatbot_output,
82
  every=200,
83
+ _js="check_keywords"
84
  )
85
 
86
+ gr.Blocks().launch(show_api=False)