wop commited on
Commit
5195c78
1 Parent(s): b48d7c8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+ import json
4
+
5
+ # Initialize the pipeline with the new model
6
+ pipe = pipeline("text-generation", model="Blexus/Quble_test_model_v1_INSTRUCT_v1")
7
+
8
+ DATABASE_PATH = "database.json"
9
+
10
+ def load_database():
11
+ try:
12
+ with open(DATABASE_PATH, "r") as file:
13
+ return json.load(file)
14
+ except FileNotFoundError:
15
+ return {}
16
+
17
+ def save_database(database):
18
+ with open(DATABASE_PATH, "w") as file:
19
+ json.dump(database, file)
20
+
21
+ def format_prompt(message, history):
22
+ # Format prompt according to the new template
23
+ prompt = "SYSTEM: You are a helpful assistant, with no access to external functions.\n<|endofsystem|>\n"
24
+ for user_prompt, bot_response in history:
25
+ prompt += f"USER: {user_prompt}\n\n\nASSISTANT: {bot_response}<|endoftext|>\n"
26
+ prompt += f"USER: {message}\n\n\nASSISTANT:"
27
+ return prompt
28
+
29
+ def generate(
30
+ prompt, history, temperature=0.9, max_new_tokens=4096, top_p=0.9, repetition_penalty=1.2,
31
+ ):
32
+ database = load_database() # Load the database
33
+ temperature = float(temperature)
34
+ if temperature < 1e-2:
35
+ temperature = 1e-2
36
+ top_p = float(top_p)
37
+
38
+ formatted_prompt = format_prompt(prompt, history)
39
+ if formatted_prompt in database:
40
+ response = database[formatted_prompt]
41
+ else:
42
+ # Use the pipeline to generate the response
43
+ response = pipe(formatted_prompt, max_new_tokens=max_new_tokens, temperature=temperature, top_p=top_p, repetition_penalty=repetition_penalty)[0]["generated_text"]
44
+ response_text = response.split("ASSISTANT:")[1].strip() # Extract the assistant's response
45
+ database[formatted_prompt] = response_text
46
+ save_database(database) # Save the updated database
47
+
48
+ yield response_text
49
+
50
+ css = """
51
+ #mkd {
52
+ height: 500px;
53
+ overflow: auto;
54
+ border: 1px solid #ccc;
55
+ }
56
+ """
57
+
58
+ with gr.Blocks(css=css) as demo:
59
+ gr.ChatInterface(
60
+ generate,
61
+ )
62
+
63
+ demo.launch(debug=True)