wop commited on
Commit
52d69de
1 Parent(s): 44b3bcb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -26
app.py CHANGED
@@ -1,26 +1,21 @@
1
  import json
2
- from huggingface_hub import InferenceClient
3
  import gradio as gr
4
  import random
 
5
 
6
  API_URL = "https://api-inference.huggingface.co/models/"
7
 
8
- client = InferenceClient(
9
- "mistralai/Mistral-7B-Instruct-v0.1"
10
- )
11
 
12
  def format_prompt(message, history):
13
  prompt = "You're a helpful assistant."
14
  for user_prompt, bot_response in history:
15
- prompt += f"[INST] {user_prompt} [/INST]"
16
- prompt += f" {bot_response}</s> "
17
- prompt += f"[INST] {message} [/INST]"
18
  return prompt
19
 
20
  def generate(prompt, history, temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0):
21
- temperature = float(temperature)
22
- if temperature < 1e-2:
23
- temperature = 1e-2
24
  top_p = float(top_p)
25
 
26
  generate_kwargs = dict(
@@ -40,47 +35,35 @@ def generate(prompt, history, temperature=0.9, max_new_tokens=2048, top_p=0.95,
40
  for response in stream:
41
  output += response.token.text
42
  yield output
43
- return output
44
 
45
  def load_database():
46
  try:
47
- # Attempt to load the database from JSON
48
  with open("database.json", "r", encoding="utf-8") as f:
49
  return json.load(f)
50
  except (FileNotFoundError, json.JSONDecodeError):
51
- # Handle potential errors gracefully
52
  print("Error loading database: File not found or invalid format. Creating an empty database.")
53
- return [] # Return an empty list if database loading fails
54
 
55
  def save_database(data):
56
  try:
57
- # Save the updated database to JSON
58
  with open("database.json", "w", encoding="utf-8") as f:
59
  json.dump(data, f, indent=4)
60
  except (IOError, json.JSONEncodeError):
61
- # Handle potential errors gracefully
62
  print("Error saving database: Encountered an issue while saving.")
63
 
64
  def chat_interface(message):
65
  database = load_database()
66
 
67
- # Check if the question already exists in the database
68
  if (message, None) not in database:
69
- # If not, generate a response and add it to the database
70
- response = generate(message, history=[])
71
  database.append((message, response))
72
  save_database(database)
73
  else:
74
- # If it does, retrieve the stored response
75
  _, stored_response = next(item for item in database if item[0] == message)
76
  response = stored_response
77
 
78
  return response
79
 
80
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
81
- # Use "Textbox" components for both input and output
82
- input_textbox = gr.Textbox(label="Your question")
83
- output_textbox = gr.Textbox(label="Assistant's response", value="", editable=False)
84
 
85
- # Use demo.launch instead of demo.queue().launch()
86
- demo.launch(fn=chat_interface, inputs=input_textbox, outputs=output_textbox)
 
1
  import json
 
2
  import gradio as gr
3
  import random
4
+ from huggingface_hub import InferenceClient
5
 
6
  API_URL = "https://api-inference.huggingface.co/models/"
7
 
8
+ client = InferenceClient("mistralai/Mistral-7B-Instruct-v0.1")
 
 
9
 
10
  def format_prompt(message, history):
11
  prompt = "You're a helpful assistant."
12
  for user_prompt, bot_response in history:
13
+ prompt += f" [INST] {user_prompt} [/INST] {bot_response}</s> "
14
+ prompt += f" [INST] {message} [/INST]"
 
15
  return prompt
16
 
17
  def generate(prompt, history, temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0):
18
+ temperature = float(temperature) if temperature > 0 else 0.01
 
 
19
  top_p = float(top_p)
20
 
21
  generate_kwargs = dict(
 
35
  for response in stream:
36
  output += response.token.text
37
  yield output
 
38
 
39
  def load_database():
40
  try:
 
41
  with open("database.json", "r", encoding="utf-8") as f:
42
  return json.load(f)
43
  except (FileNotFoundError, json.JSONDecodeError):
 
44
  print("Error loading database: File not found or invalid format. Creating an empty database.")
45
+ return []
46
 
47
  def save_database(data):
48
  try:
 
49
  with open("database.json", "w", encoding="utf-8") as f:
50
  json.dump(data, f, indent=4)
51
  except (IOError, json.JSONEncodeError):
 
52
  print("Error saving database: Encountered an issue while saving.")
53
 
54
  def chat_interface(message):
55
  database = load_database()
56
 
 
57
  if (message, None) not in database:
58
+ response = next(generate(message, history=[]))
 
59
  database.append((message, response))
60
  save_database(database)
61
  else:
 
62
  _, stored_response = next(item for item in database if item[0] == message)
63
  response = stored_response
64
 
65
  return response
66
 
67
+ with gr.Interface(fn=chat_interface, inputs="textbox", outputs="textbox", title="Chat Interface") as iface:
68
+ iface.launch()
 
 
69