wop commited on
Commit
0af0fc5
1 Parent(s): b9ba584

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -17
app.py CHANGED
@@ -19,46 +19,42 @@ def save_database(database):
19
  json.dump(database, file)
20
 
21
  def format_prompt(message, system, history):
22
- # Format prompt according to the new template
23
  prompt = f"SYSTEM: {system}\n<|endofsystem|>\n"
24
 
25
  for entry in history:
26
- if len(entry) == 2: # Check if the history contains exactly two items
27
  user_prompt, bot_response = entry
28
  prompt += f"USER: {user_prompt}\n\n\nASSISTANT: {bot_response}<|endoftext|>\n"
29
- else:
30
- # If the history doesn't match the expected format, handle it gracefully
31
- continue
32
 
33
  prompt += f"USER: {message}\n\n\nASSISTANT:"
34
  return prompt
35
 
36
- def generate(
37
- prompt, system, history, temperature=0.9, max_new_tokens=4096, top_p=0.9, repetition_penalty=1.2,
38
- ):
39
- database = load_database() # Load the database
40
  temperature = float(temperature)
41
  if temperature < 1e-2:
42
  temperature = 1e-2
43
  top_p = float(top_p)
44
 
45
  formatted_prompt = format_prompt(prompt, system, history)
46
- response_text = "We are sorry but Quble doesnt know how to answer." # Default response
47
 
48
  if formatted_prompt in database:
49
  response_text = database[formatted_prompt]
50
  else:
51
  # Use the pipeline to generate the response
52
  try:
53
- response = pipe(formatted_prompt, max_new_tokens=max_new_tokens, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty)[0]["generated_text"]
54
- response_text = response.split("ASSISTANT:")[1].strip() # Extract the assistant's response
55
- database[formatted_prompt] = response_text
56
- save_database(database) # Save the updated database
 
 
 
 
57
  except Exception as e:
58
  print(f"Error generating response: {e}")
59
 
60
- yield response_text
61
-
62
  customCSS = """
63
  #component-7 { # this is the default element ID of the chat component
64
  height: 1600px; # adjust the height as needed
@@ -66,7 +62,7 @@ customCSS = """
66
  }
67
  """
68
 
69
- additional_inputs=[
70
  gr.Textbox(
71
  label="System prompt",
72
  value="You are a helpful assistant, with no access to external functions.",
 
19
  json.dump(database, file)
20
 
21
  def format_prompt(message, system, history):
 
22
  prompt = f"SYSTEM: {system}\n<|endofsystem|>\n"
23
 
24
  for entry in history:
25
+ if len(entry) == 2:
26
  user_prompt, bot_response = entry
27
  prompt += f"USER: {user_prompt}\n\n\nASSISTANT: {bot_response}<|endoftext|>\n"
 
 
 
28
 
29
  prompt += f"USER: {message}\n\n\nASSISTANT:"
30
  return prompt
31
 
32
+ def generate(prompt, system, history, temperature=0.9, max_new_tokens=4096, top_p=0.9, repetition_penalty=1.2):
33
+ database = load_database()
 
 
34
  temperature = float(temperature)
35
  if temperature < 1e-2:
36
  temperature = 1e-2
37
  top_p = float(top_p)
38
 
39
  formatted_prompt = format_prompt(prompt, system, history)
40
+ response_text = "We are sorry but Quble doesn't know how to answer."
41
 
42
  if formatted_prompt in database:
43
  response_text = database[formatted_prompt]
44
  else:
45
  # Use the pipeline to generate the response
46
  try:
47
+ # Stream the response
48
+ for response in pipe(formatted_prompt, max_new_tokens=max_new_tokens, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty, return_full_text=False, streaming=True):
49
+ assistant_response = response["generated_text"].split("ASSISTANT:")[-1].strip()
50
+ yield assistant_response
51
+
52
+ # Save the generated response to the database after the stream
53
+ database[formatted_prompt] = assistant_response
54
+ save_database(database)
55
  except Exception as e:
56
  print(f"Error generating response: {e}")
57
 
 
 
58
  customCSS = """
59
  #component-7 { # this is the default element ID of the chat component
60
  height: 1600px; # adjust the height as needed
 
62
  }
63
  """
64
 
65
+ additional_inputs = [
66
  gr.Textbox(
67
  label="System prompt",
68
  value="You are a helpful assistant, with no access to external functions.",