admin08077 commited on
Commit
9e910c5
·
verified ·
1 Parent(s): 4c8d943

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -20
app.py CHANGED
@@ -1,12 +1,39 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
 
3
 
4
  """
5
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
  """
7
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def respond(
11
  message,
12
  history: list[tuple[str, str]],
@@ -25,8 +52,15 @@ def respond(
25
 
26
  messages.append({"role": "user", "content": message})
27
 
28
- response = ""
 
 
 
 
 
29
 
 
 
30
  for message in client.chat_completion(
31
  messages,
32
  max_tokens=max_tokens,
@@ -35,30 +69,65 @@ def respond(
35
  top_p=top_p,
36
  ):
37
  token = message.choices[0].delta.content
38
-
39
  response += token
40
  yield response
41
 
 
 
42
 
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- demo = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
  if __name__ == "__main__":
64
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ import json
4
+ import os
5
 
6
  """
7
  For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
8
  """
9
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
10
 
11
+ # Persistent memory and knowledge base setup
12
+ memory_file = "chat_memory.json"
13
+ knowledge_base = {
14
+ "AI": "Artificial Intelligence is a branch of computer science that focuses on creating systems capable of performing tasks that typically require human intelligence.",
15
+ "Quantum Computing": "Quantum computing is a type of computation that uses quantum mechanics to process information in ways classical computers cannot.",
16
+ }
17
 
18
+ # Load memory from file
19
+ def load_memory():
20
+ if os.path.exists(memory_file):
21
+ with open(memory_file, "r") as f:
22
+ return json.load(f)
23
+ return []
24
+
25
+ # Save memory to file
26
+ def save_memory(memory):
27
+ with open(memory_file, "w") as f:
28
+ json.dump(memory, f)
29
+
30
+ # Append to memory
31
+ def update_memory(conversation):
32
+ memory = load_memory()
33
+ memory.append(conversation)
34
+ save_memory(memory)
35
+
36
+ # Response generation with memory and knowledge base integration
37
  def respond(
38
  message,
39
  history: list[tuple[str, str]],
 
52
 
53
  messages.append({"role": "user", "content": message})
54
 
55
+ # Check for answers in the knowledge base
56
+ if message in knowledge_base:
57
+ response = knowledge_base[message]
58
+ update_memory((message, response))
59
+ yield response
60
+ return
61
 
62
+ # Generate response from AI
63
+ response = ""
64
  for message in client.chat_completion(
65
  messages,
66
  max_tokens=max_tokens,
 
69
  top_p=top_p,
70
  ):
71
  token = message.choices[0].delta.content
 
72
  response += token
73
  yield response
74
 
75
+ # Update memory
76
+ update_memory((message, response))
77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
+ # Gradio interface with enhanced functionality
80
+ def add_to_knowledge_base(key, value):
81
+ knowledge_base[key] = value
82
+ return f"Added to knowledge base: {key} -> {value}"
83
+
84
+
85
+ demo = gr.Blocks()
86
+
87
+ with demo:
88
+ gr.Markdown("# Advanced Chatbot with Memory and Knowledge Base")
89
+
90
+ with gr.Tab("Chat"):
91
+ chatbot = gr.ChatInterface(
92
+ respond,
93
+ additional_inputs=[
94
+ gr.Textbox(value="You are an advanced AI Chatbot.", label="System message"),
95
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
96
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
97
+ gr.Slider(
98
+ minimum=0.1,
99
+ maximum=1.0,
100
+ value=0.95,
101
+ step=0.05,
102
+ label="Top-p (nucleus sampling)",
103
+ ),
104
+ ],
105
+ )
106
+
107
+ with gr.Tab("Knowledge Base"):
108
+ gr.Markdown("### Manage the Knowledge Base")
109
+ kb_key = gr.Textbox(label="Key", placeholder="Enter the topic or question")
110
+ kb_value = gr.Textbox(label="Value", placeholder="Enter the explanation or answer")
111
+ add_kb_button = gr.Button("Add to Knowledge Base")
112
+ kb_output = gr.Textbox(label="Knowledge Base Output")
113
+
114
+ add_kb_button.click(add_to_knowledge_base, [kb_key, kb_value], kb_output)
115
+
116
+ with gr.Tab("Memory"):
117
+ gr.Markdown("### Conversation Memory")
118
+ memory_display = gr.Textbox(label="Conversation Memory", lines=10)
119
+ refresh_memory = gr.Button("Refresh Memory")
120
+ clear_memory = gr.Button("Clear Memory")
121
+
122
+ def display_memory():
123
+ return json.dumps(load_memory(), indent=2)
124
+
125
+ def clear_memory_func():
126
+ save_memory([])
127
+ return "Memory Cleared!"
128
+
129
+ refresh_memory.click(display_memory, outputs=memory_display)
130
+ clear_memory.click(clear_memory_func, outputs=memory_display)
131
 
132
  if __name__ == "__main__":
133
  demo.launch()