Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import HfApi, login, upload_file
|
| 3 |
|
| 4 |
-
# Local storage for rules
|
| 5 |
rules = {}
|
| 6 |
|
| 7 |
-
# Load rules from a file at startup
|
| 8 |
def load_rules():
|
| 9 |
-
# Download rules file from HF dataset/repo (use huggingface_hub or requests)
|
| 10 |
global rules
|
| 11 |
try:
|
| 12 |
with open("rules.txt", "r") as f:
|
| 13 |
lines = f.readlines()
|
| 14 |
-
rules = dict(line.strip().split("=>") for line in lines if "=>" in line)
|
| 15 |
-
except Exception
|
| 16 |
rules = {}
|
| 17 |
|
| 18 |
def chatbot_response(message, history):
|
| 19 |
-
#
|
| 20 |
for k, v in rules.items():
|
| 21 |
if k in message:
|
| 22 |
return v
|
| 23 |
return "No matching rule. Submit an example!"
|
| 24 |
|
| 25 |
def submit_rule(user_input, rule_response, write_token):
|
| 26 |
-
# Add new rule
|
| 27 |
global rules
|
| 28 |
rules[user_input] = rule_response
|
| 29 |
-
# Save locally
|
| 30 |
with open("rules.txt", "a") as f:
|
| 31 |
f.write(f"{user_input}=>{rule_response}\n")
|
| 32 |
# Upload using user's write token
|
|
@@ -45,15 +40,19 @@ def submit_rule(user_input, rule_response, write_token):
|
|
| 45 |
load_rules()
|
| 46 |
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
-
chat = gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
| 49 |
rule_input = gr.Textbox(label="User Example")
|
| 50 |
rule_output = gr.Textbox(label="Response")
|
| 51 |
token_input = gr.Textbox(label="Write Token", type="password")
|
| 52 |
submit = gr.Button("Submit Rule")
|
|
|
|
| 53 |
submit.click(
|
| 54 |
submit_rule,
|
| 55 |
inputs=[rule_input, rule_output, token_input],
|
| 56 |
-
outputs=
|
| 57 |
)
|
| 58 |
|
| 59 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import HfApi, login, upload_file
|
| 3 |
|
|
|
|
| 4 |
rules = {}
|
| 5 |
|
|
|
|
| 6 |
def load_rules():
|
|
|
|
| 7 |
global rules
|
| 8 |
try:
|
| 9 |
with open("rules.txt", "r") as f:
|
| 10 |
lines = f.readlines()
|
| 11 |
+
rules = dict(line.strip().split("=>", 1) for line in lines if "=>" in line)
|
| 12 |
+
except Exception:
|
| 13 |
rules = {}
|
| 14 |
|
| 15 |
def chatbot_response(message, history):
|
| 16 |
+
# message is a string, history is now a list of dicts in "messages" format
|
| 17 |
for k, v in rules.items():
|
| 18 |
if k in message:
|
| 19 |
return v
|
| 20 |
return "No matching rule. Submit an example!"
|
| 21 |
|
| 22 |
def submit_rule(user_input, rule_response, write_token):
|
|
|
|
| 23 |
global rules
|
| 24 |
rules[user_input] = rule_response
|
|
|
|
| 25 |
with open("rules.txt", "a") as f:
|
| 26 |
f.write(f"{user_input}=>{rule_response}\n")
|
| 27 |
# Upload using user's write token
|
|
|
|
| 40 |
load_rules()
|
| 41 |
|
| 42 |
with gr.Blocks() as demo:
|
| 43 |
+
chat = gr.ChatInterface(
|
| 44 |
+
fn=chatbot_response,
|
| 45 |
+
type="messages" # Use the recommended format
|
| 46 |
+
)
|
| 47 |
rule_input = gr.Textbox(label="User Example")
|
| 48 |
rule_output = gr.Textbox(label="Response")
|
| 49 |
token_input = gr.Textbox(label="Write Token", type="password")
|
| 50 |
submit = gr.Button("Submit Rule")
|
| 51 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
| 52 |
submit.click(
|
| 53 |
submit_rule,
|
| 54 |
inputs=[rule_input, rule_output, token_input],
|
| 55 |
+
outputs=status_output
|
| 56 |
)
|
| 57 |
|
| 58 |
demo.launch()
|