Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,10 +8,21 @@ tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2")
|
|
| 8 |
# Set pad_token as eos_token
|
| 9 |
tokenizer.pad_token = tokenizer.eos_token
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Chatbot function
|
| 12 |
def chatbot(input_text):
|
|
|
|
|
|
|
|
|
|
| 13 |
inputs = tokenizer(
|
| 14 |
-
|
| 15 |
return_tensors="pt",
|
| 16 |
padding=True,
|
| 17 |
truncation=True
|
|
@@ -23,6 +34,11 @@ def chatbot(input_text):
|
|
| 23 |
pad_token_id=tokenizer.pad_token_id
|
| 24 |
)
|
| 25 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
return response
|
| 27 |
|
| 28 |
# Gradio Interface
|
|
@@ -31,8 +47,8 @@ iface = gr.Interface(
|
|
| 31 |
inputs="text",
|
| 32 |
outputs="text",
|
| 33 |
title="AeroAI Chatbot (Phi-2)",
|
| 34 |
-
description="Chat with AeroAI powered by Phi-2!"
|
| 35 |
)
|
| 36 |
|
| 37 |
# Launch the interface
|
| 38 |
-
iface.launch()
|
|
|
|
| 8 |
# Set pad_token as eos_token
|
| 9 |
tokenizer.pad_token = tokenizer.eos_token
|
| 10 |
|
| 11 |
+
# Load training protocol from file
|
| 12 |
+
try:
|
| 13 |
+
with open("training-protocol.aero", "r", encoding="utf-8") as f:
|
| 14 |
+
training_protocol = f.read().strip()
|
| 15 |
+
except FileNotFoundError:
|
| 16 |
+
training_protocol = "You are AeroAI, a helpful, friendly, and slightly humorous educational assistant."
|
| 17 |
+
print("⚠ training-protocol.aero not found, using default protocol.")
|
| 18 |
+
|
| 19 |
# Chatbot function
|
| 20 |
def chatbot(input_text):
|
| 21 |
+
# Combine protocol with user input
|
| 22 |
+
prompt = f"{training_protocol}\n\nUser: {input_text}\nAeroAI:"
|
| 23 |
+
|
| 24 |
inputs = tokenizer(
|
| 25 |
+
prompt,
|
| 26 |
return_tensors="pt",
|
| 27 |
padding=True,
|
| 28 |
truncation=True
|
|
|
|
| 34 |
pad_token_id=tokenizer.pad_token_id
|
| 35 |
)
|
| 36 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 37 |
+
|
| 38 |
+
# Optional: cut out the protocol part from the output
|
| 39 |
+
if "AeroAI:" in response:
|
| 40 |
+
response = response.split("AeroAI:")[-1].strip()
|
| 41 |
+
|
| 42 |
return response
|
| 43 |
|
| 44 |
# Gradio Interface
|
|
|
|
| 47 |
inputs="text",
|
| 48 |
outputs="text",
|
| 49 |
title="AeroAI Chatbot (Phi-2)",
|
| 50 |
+
description="Chat with AeroAI powered by Phi-2! Persona loaded from training-protocol.aero."
|
| 51 |
)
|
| 52 |
|
| 53 |
# Launch the interface
|
| 54 |
+
iface.launch()
|