Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -37,15 +37,10 @@ def clear_memory():
|
|
| 37 |
def chat_engine(message):
|
| 38 |
history = load_history()
|
| 39 |
|
| 40 |
-
|
| 41 |
-
messages = [{"role": "system", "content": "You are a helpful AI assistant. Answer directly and concisely."}]
|
| 42 |
-
|
| 43 |
-
# Add History
|
| 44 |
for turn in history:
|
| 45 |
messages.append({"role": "user", "content": str(turn['user'])})
|
| 46 |
messages.append({"role": "assistant", "content": str(turn['bot'])})
|
| 47 |
-
|
| 48 |
-
# Add Current Message
|
| 49 |
messages.append({"role": "user", "content": str(message)})
|
| 50 |
|
| 51 |
try:
|
|
@@ -53,11 +48,8 @@ def chat_engine(message):
|
|
| 53 |
messages, max_tokens=512, stream=False, temperature=0.7
|
| 54 |
)
|
| 55 |
bot_reply = response.choices[0].message.content
|
| 56 |
-
|
| 57 |
-
# Save History
|
| 58 |
history.append({"user": message, "bot": bot_reply})
|
| 59 |
save_history(history)
|
| 60 |
-
|
| 61 |
return bot_reply
|
| 62 |
except Exception as e:
|
| 63 |
return f"⚠️ Error: {str(e)}"
|
|
@@ -76,24 +68,20 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 76 |
gr.Markdown("# Nexus AI Backend")
|
| 77 |
|
| 78 |
with gr.Tab("Chat"):
|
| 79 |
-
# Visual Chatbot
|
| 80 |
gr.ChatInterface(fn=lambda m, h: chat_engine(m))
|
| 81 |
|
| 82 |
-
#
|
| 83 |
clear_btn = gr.Button("🗑️ Clear Server Memory")
|
| 84 |
status_msg = gr.Markdown("")
|
| 85 |
-
clear_btn.click(fn=clear_memory, inputs=[], outputs=[status_msg])
|
| 86 |
|
| 87 |
-
#
|
|
|
|
|
|
|
|
|
|
| 88 |
hidden_input = gr.Textbox(visible=False)
|
| 89 |
hidden_output = gr.Textbox(visible=False)
|
| 90 |
api_btn = gr.Button(visible=False)
|
| 91 |
-
api_btn.click(
|
| 92 |
-
fn=chat_engine,
|
| 93 |
-
inputs=[hidden_input],
|
| 94 |
-
outputs=[hidden_output],
|
| 95 |
-
api_name="chat_engine"
|
| 96 |
-
)
|
| 97 |
|
| 98 |
with gr.Tab("Image"):
|
| 99 |
i_in = gr.Textbox(label="Prompt")
|
|
|
|
| 37 |
def chat_engine(message):
|
| 38 |
history = load_history()
|
| 39 |
|
| 40 |
+
messages = [{"role": "system", "content": "You are a helpful AI assistant."}]
|
|
|
|
|
|
|
|
|
|
| 41 |
for turn in history:
|
| 42 |
messages.append({"role": "user", "content": str(turn['user'])})
|
| 43 |
messages.append({"role": "assistant", "content": str(turn['bot'])})
|
|
|
|
|
|
|
| 44 |
messages.append({"role": "user", "content": str(message)})
|
| 45 |
|
| 46 |
try:
|
|
|
|
| 48 |
messages, max_tokens=512, stream=False, temperature=0.7
|
| 49 |
)
|
| 50 |
bot_reply = response.choices[0].message.content
|
|
|
|
|
|
|
| 51 |
history.append({"user": message, "bot": bot_reply})
|
| 52 |
save_history(history)
|
|
|
|
| 53 |
return bot_reply
|
| 54 |
except Exception as e:
|
| 55 |
return f"⚠️ Error: {str(e)}"
|
|
|
|
| 68 |
gr.Markdown("# Nexus AI Backend")
|
| 69 |
|
| 70 |
with gr.Tab("Chat"):
|
|
|
|
| 71 |
gr.ChatInterface(fn=lambda m, h: chat_engine(m))
|
| 72 |
|
| 73 |
+
# --- MEMORY CONTROL (Updated) ---
|
| 74 |
clear_btn = gr.Button("🗑️ Clear Server Memory")
|
| 75 |
status_msg = gr.Markdown("")
|
|
|
|
| 76 |
|
| 77 |
+
# WE ADDED api_name="clear_memory" HERE:
|
| 78 |
+
clear_btn.click(fn=clear_memory, inputs=[], outputs=[status_msg], api_name="clear_memory")
|
| 79 |
+
|
| 80 |
+
# HIDDEN API
|
| 81 |
hidden_input = gr.Textbox(visible=False)
|
| 82 |
hidden_output = gr.Textbox(visible=False)
|
| 83 |
api_btn = gr.Button(visible=False)
|
| 84 |
+
api_btn.click(fn=chat_engine, inputs=[hidden_input], outputs=[hidden_output], api_name="chat_engine")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
with gr.Tab("Image"):
|
| 87 |
i_in = gr.Textbox(label="Prompt")
|