Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -43,7 +43,7 @@ memory = load_memory()
|
|
| 43 |
# ----------------
|
| 44 |
# Chat function
|
| 45 |
# ----------------
|
| 46 |
-
def chat_with_model(message, history, context
|
| 47 |
if not isinstance(history, list):
|
| 48 |
history = []
|
| 49 |
|
|
@@ -55,9 +55,6 @@ def chat_with_model(message, history, context, file_input=None):
|
|
| 55 |
save_memory(history)
|
| 56 |
return history, history
|
| 57 |
|
| 58 |
-
if file_input:
|
| 59 |
-
message += f"\n\n๐ Uploaded file: {file_input.name}"
|
| 60 |
-
|
| 61 |
conversation = [
|
| 62 |
{"role": "system", "content": (
|
| 63 |
"You are EduAI, a multilingual educational AI assistant created by a Sri Lankan student named Wafa Fazly. "
|
|
@@ -79,77 +76,4 @@ def chat_with_model(message, history, context, file_input=None):
|
|
| 79 |
data = response.json()
|
| 80 |
reply = data["choices"][0]["message"]["content"]
|
| 81 |
|
| 82 |
-
reply = reply.replace("Step
|
| 83 |
-
history.append({"role": "user", "content": message})
|
| 84 |
-
history.append({"role": "assistant", "content": reply})
|
| 85 |
-
save_memory(history)
|
| 86 |
-
return history, history
|
| 87 |
-
|
| 88 |
-
except Exception as e:
|
| 89 |
-
history.append({"role": "user", "content": message})
|
| 90 |
-
history.append({"role": "assistant", "content": "๐
EduAI is having trouble connecting."})
|
| 91 |
-
return history, history
|
| 92 |
-
|
| 93 |
-
# ----------------
|
| 94 |
-
# Sidebar helpers
|
| 95 |
-
# ----------------
|
| 96 |
-
def update_context(choice):
|
| 97 |
-
if not choice:
|
| 98 |
-
return "๐ **General Mode.** Ask EduAI anything!"
|
| 99 |
-
return f"๐ **{choice} mode selected.**"
|
| 100 |
-
|
| 101 |
-
def clear_memory():
|
| 102 |
-
if os.path.exists(MEMORY_FILE):
|
| 103 |
-
os.remove(MEMORY_FILE)
|
| 104 |
-
return [], "๐งน Chat memory cleared."
|
| 105 |
-
|
| 106 |
-
# ----------------
|
| 107 |
-
# Gradio UI
|
| 108 |
-
# ----------------
|
| 109 |
-
with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as iface:
|
| 110 |
-
gr.Markdown("# ๐ EduAI โ Professional Study Companion")
|
| 111 |
-
|
| 112 |
-
with gr.Row():
|
| 113 |
-
# Sidebar
|
| 114 |
-
with gr.Column(scale=1, min_width=250):
|
| 115 |
-
gr.Markdown("### ๐งญ Main Menu")
|
| 116 |
-
|
| 117 |
-
with gr.Accordion("๐ Subject Tutor", open=False):
|
| 118 |
-
subj = gr.Radio(["Science ๐งช", "ICT ๐ป", "English ๐", "Math โ"], label="Subject")
|
| 119 |
-
|
| 120 |
-
with gr.Accordion("๐ Study Planner", open=False):
|
| 121 |
-
planner = gr.Radio(["View Plan ๐
", "Add Task โ๏ธ", "Study Tips ๐ก"], label="Planner")
|
| 122 |
-
|
| 123 |
-
with gr.Accordion("๐ Languages", open=False):
|
| 124 |
-
lang = gr.Radio(["Sinhala ๐ฑ๐ฐ", "Tamil ๐ฎ๐ณ", "English ๐ฌ๐ง", "Spanish ๐ช๐ธ"], label="Language")
|
| 125 |
-
|
| 126 |
-
with gr.Accordion("โ๏ธ Settings", open=False):
|
| 127 |
-
clear_btn = gr.Button("๐งน Clear Memory")
|
| 128 |
-
|
| 129 |
-
# Chat area
|
| 130 |
-
with gr.Column(scale=4):
|
| 131 |
-
context_display = gr.Markdown("๐ **General Mode.** Ask EduAI anything!")
|
| 132 |
-
|
| 133 |
-
chatbot = gr.Chatbot(
|
| 134 |
-
label="EduAI Chat",
|
| 135 |
-
height=450,
|
| 136 |
-
type="messages",
|
| 137 |
-
render_markdown=True,
|
| 138 |
-
latex_delimiters=[{"left": "$$", "right": "$$", "display": True},
|
| 139 |
-
{"left": "\\[", "right": "\\]", "display": True}]
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
with gr.Row():
|
| 143 |
-
msg = gr.Textbox(placeholder="Type your question here...", show_label=False)
|
| 144 |
-
file_input = gr.File(file_types=[".pdf", ".docx", ".png", ".jpg"], show_label=False)
|
| 145 |
-
send_btn = gr.Button("โ๏ธ")
|
| 146 |
-
|
| 147 |
-
# Events
|
| 148 |
-
subj.change(update_context, inputs=subj, outputs=context_display)
|
| 149 |
-
planner.change(update_context, inputs=planner, outputs=context_display)
|
| 150 |
-
lang.change(update_context, inputs=lang, outputs=context_display)
|
| 151 |
-
send_btn.click(chat_with_model, inputs=[msg, chatbot, context_display, file_input], outputs=[chatbot, chatbot])
|
| 152 |
-
msg.submit(chat_with_model, inputs=[msg, chatbot, context_display, file_input], outputs=[chatbot, chatbot])
|
| 153 |
-
clear_btn.click(clear_memory, outputs=[chatbot, context_display])
|
| 154 |
-
|
| 155 |
-
iface.launch()
|
|
|
|
| 43 |
# ----------------
|
| 44 |
# Chat function
|
| 45 |
# ----------------
|
| 46 |
+
def chat_with_model(message, history, context):
|
| 47 |
if not isinstance(history, list):
|
| 48 |
history = []
|
| 49 |
|
|
|
|
| 55 |
save_memory(history)
|
| 56 |
return history, history
|
| 57 |
|
|
|
|
|
|
|
|
|
|
| 58 |
conversation = [
|
| 59 |
{"role": "system", "content": (
|
| 60 |
"You are EduAI, a multilingual educational AI assistant created by a Sri Lankan student named Wafa Fazly. "
|
|
|
|
| 76 |
data = response.json()
|
| 77 |
reply = data["choices"][0]["message"]["content"]
|
| 78 |
|
| 79 |
+
reply = reply.replace("Step
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|