Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from openai import OpenAI
|
|
| 8 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 9 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 10 |
|
| 11 |
-
SYSTEM_PROMPT = """Create an intelligent Python chatbot capable of engaging in natural... (
|
| 12 |
"""
|
| 13 |
|
| 14 |
def init_messages():
|
|
@@ -53,9 +53,8 @@ def respond(user_text, chat_history, messages):
|
|
| 53 |
|
| 54 |
|
| 55 |
# -----------------------------
|
| 56 |
-
#
|
| 57 |
# -----------------------------
|
| 58 |
-
|
| 59 |
FAQ_QUESTIONS = [
|
| 60 |
"What is the difference between a list, tuple, and set in Python?",
|
| 61 |
"How do I use dictionaries effectively in Python?",
|
|
@@ -81,14 +80,19 @@ css = """
|
|
| 81 |
.header-subtitle {font-size: 12.5px; opacity: 0.75; margin-top: 2px;}
|
| 82 |
.faq-box {border: 1px solid rgba(255,255,255,0.08); border-radius: 12px; padding: 14px;}
|
| 83 |
.faq-btn button {width: 100%; justify-content: flex-start;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
"""
|
| 85 |
|
| 86 |
with gr.Blocks(elem_id="app_container") as demo:
|
| 87 |
|
| 88 |
-
# Header
|
| 89 |
with gr.Row():
|
| 90 |
with gr.Column(scale=1, min_width=80):
|
| 91 |
-
|
| 92 |
value=LOGO_URL,
|
| 93 |
label=None,
|
| 94 |
show_label=False,
|
|
@@ -96,37 +100,36 @@ with gr.Blocks(elem_id="app_container") as demo:
|
|
| 96 |
width=64,
|
| 97 |
container=False
|
| 98 |
)
|
|
|
|
| 99 |
with gr.Column(scale=10):
|
| 100 |
gr.HTML("""
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
</div>
|
| 107 |
</div>
|
| 108 |
</div>
|
|
|
|
| 109 |
""")
|
| 110 |
|
| 111 |
gr.Markdown("---")
|
| 112 |
|
|
|
|
| 113 |
state = gr.State(init_messages())
|
| 114 |
|
| 115 |
with gr.Row(equal_height=True):
|
| 116 |
|
| 117 |
-
# LEFT
|
| 118 |
with gr.Column(scale=4, min_width=320):
|
| 119 |
with gr.Group(elem_classes=["faq-box"]):
|
| 120 |
gr.Markdown("### FAQ — Most Asked Python Questions")
|
| 121 |
-
|
| 122 |
-
gr.Markdown(
|
| 123 |
-
"Click a question to auto-fill it, then press Enter or Send."
|
| 124 |
-
)
|
| 125 |
|
| 126 |
faq_buttons = []
|
| 127 |
for q in FAQ_QUESTIONS:
|
| 128 |
-
|
| 129 |
-
faq_buttons.append(
|
| 130 |
|
| 131 |
gr.Markdown("### Quick prompt ideas")
|
| 132 |
quick = gr.Radio(
|
|
@@ -141,9 +144,13 @@ with gr.Blocks(elem_id="app_container") as demo:
|
|
| 141 |
value=None
|
| 142 |
)
|
| 143 |
|
| 144 |
-
# RIGHT
|
| 145 |
with gr.Column(scale=8, min_width=520):
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
with gr.Row():
|
| 149 |
msg = gr.Textbox(
|
|
@@ -156,34 +163,31 @@ with gr.Blocks(elem_id="app_container") as demo:
|
|
| 156 |
with gr.Row():
|
| 157 |
clear = gr.Button("Clear Chat")
|
| 158 |
gr.Markdown(
|
| 159 |
-
"<span style='opacity:0.7;font-size:12px;'>Context is preserved unless
|
| 160 |
)
|
| 161 |
|
| 162 |
-
# FAQ buttons
|
| 163 |
for b, q in zip(faq_buttons, FAQ_QUESTIONS):
|
| 164 |
b.click(lambda q=q: set_question(q), None, msg)
|
| 165 |
|
| 166 |
# Quick preference
|
| 167 |
-
def
|
| 168 |
if not pref:
|
| 169 |
return current_text
|
| 170 |
if current_text and current_text.strip():
|
| 171 |
return f"{current_text.strip()} ({pref})"
|
| 172 |
return pref
|
| 173 |
|
| 174 |
-
quick.change(
|
| 175 |
|
| 176 |
-
# Send
|
| 177 |
msg.submit(respond, [msg, chatbot, state], [msg, chatbot, state])
|
| 178 |
send.click(respond, [msg, chatbot, state], [msg, chatbot, state])
|
| 179 |
|
| 180 |
-
# Clear
|
| 181 |
clear.click(clear_all, None, [chatbot, state, msg])
|
| 182 |
|
| 183 |
-
|
| 184 |
-
# -----------------------------
|
| 185 |
-
# Must move theme + css here for Gradio 6.x
|
| 186 |
-
# -----------------------------
|
| 187 |
demo.launch(
|
| 188 |
theme=gr.themes.Soft(),
|
| 189 |
css=css,
|
|
|
|
| 8 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
| 9 |
client = OpenAI(api_key=OPENAI_API_KEY)
|
| 10 |
|
| 11 |
+
SYSTEM_PROMPT = """Create an intelligent Python chatbot capable of engaging in natural... (your full text here)
|
| 12 |
"""
|
| 13 |
|
| 14 |
def init_messages():
|
|
|
|
| 53 |
|
| 54 |
|
| 55 |
# -----------------------------
|
| 56 |
+
# UI Content
|
| 57 |
# -----------------------------
|
|
|
|
| 58 |
FAQ_QUESTIONS = [
|
| 59 |
"What is the difference between a list, tuple, and set in Python?",
|
| 60 |
"How do I use dictionaries effectively in Python?",
|
|
|
|
| 80 |
.header-subtitle {font-size: 12.5px; opacity: 0.75; margin-top: 2px;}
|
| 81 |
.faq-box {border: 1px solid rgba(255,255,255,0.08); border-radius: 12px; padding: 14px;}
|
| 82 |
.faq-btn button {width: 100%; justify-content: flex-start;}
|
| 83 |
+
|
| 84 |
+
/* Optional: nicer chat bubble widths */
|
| 85 |
+
.gr-chatbot .message {
|
| 86 |
+
max-width: 85% !important;
|
| 87 |
+
}
|
| 88 |
"""
|
| 89 |
|
| 90 |
with gr.Blocks(elem_id="app_container") as demo:
|
| 91 |
|
| 92 |
+
# Header Banner
|
| 93 |
with gr.Row():
|
| 94 |
with gr.Column(scale=1, min_width=80):
|
| 95 |
+
gr.Image(
|
| 96 |
value=LOGO_URL,
|
| 97 |
label=None,
|
| 98 |
show_label=False,
|
|
|
|
| 100 |
width=64,
|
| 101 |
container=False
|
| 102 |
)
|
| 103 |
+
|
| 104 |
with gr.Column(scale=10):
|
| 105 |
gr.HTML("""
|
| 106 |
+
<div class="header-wrap">
|
| 107 |
+
<div>
|
| 108 |
+
<div class="header-title">Python Tutor Bot</div>
|
| 109 |
+
<div class="header-subtitle">
|
| 110 |
+
Ask anything about Python — concepts, debugging, best practices, and examples.
|
|
|
|
| 111 |
</div>
|
| 112 |
</div>
|
| 113 |
+
</div>
|
| 114 |
""")
|
| 115 |
|
| 116 |
gr.Markdown("---")
|
| 117 |
|
| 118 |
+
# State
|
| 119 |
state = gr.State(init_messages())
|
| 120 |
|
| 121 |
with gr.Row(equal_height=True):
|
| 122 |
|
| 123 |
+
# LEFT SIDE — FAQ
|
| 124 |
with gr.Column(scale=4, min_width=320):
|
| 125 |
with gr.Group(elem_classes=["faq-box"]):
|
| 126 |
gr.Markdown("### FAQ — Most Asked Python Questions")
|
| 127 |
+
gr.Markdown("Click a question to auto-fill it.")
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
faq_buttons = []
|
| 130 |
for q in FAQ_QUESTIONS:
|
| 131 |
+
btn = gr.Button(q, elem_classes=["faq-btn"])
|
| 132 |
+
faq_buttons.append(btn)
|
| 133 |
|
| 134 |
gr.Markdown("### Quick prompt ideas")
|
| 135 |
quick = gr.Radio(
|
|
|
|
| 144 |
value=None
|
| 145 |
)
|
| 146 |
|
| 147 |
+
# RIGHT SIDE — CHAT
|
| 148 |
with gr.Column(scale=8, min_width=520):
|
| 149 |
+
|
| 150 |
+
chatbot = gr.Chatbot(
|
| 151 |
+
height=520,
|
| 152 |
+
label="Conversation"
|
| 153 |
+
)
|
| 154 |
|
| 155 |
with gr.Row():
|
| 156 |
msg = gr.Textbox(
|
|
|
|
| 163 |
with gr.Row():
|
| 164 |
clear = gr.Button("Clear Chat")
|
| 165 |
gr.Markdown(
|
| 166 |
+
"<span style='opacity:0.7;font-size:12px;'>Context is preserved unless cleared.</span>"
|
| 167 |
)
|
| 168 |
|
| 169 |
+
# FAQ buttons
|
| 170 |
for b, q in zip(faq_buttons, FAQ_QUESTIONS):
|
| 171 |
b.click(lambda q=q: set_question(q), None, msg)
|
| 172 |
|
| 173 |
# Quick preference
|
| 174 |
+
def apply_pref(pref, current_text):
|
| 175 |
if not pref:
|
| 176 |
return current_text
|
| 177 |
if current_text and current_text.strip():
|
| 178 |
return f"{current_text.strip()} ({pref})"
|
| 179 |
return pref
|
| 180 |
|
| 181 |
+
quick.change(apply_pref, [quick, msg], msg)
|
| 182 |
|
| 183 |
+
# Send message
|
| 184 |
msg.submit(respond, [msg, chatbot, state], [msg, chatbot, state])
|
| 185 |
send.click(respond, [msg, chatbot, state], [msg, chatbot, state])
|
| 186 |
|
| 187 |
+
# Clear chat
|
| 188 |
clear.click(clear_all, None, [chatbot, state, msg])
|
| 189 |
|
| 190 |
+
# Launch with theme + css (Gradio 6 requirement)
|
|
|
|
|
|
|
|
|
|
| 191 |
demo.launch(
|
| 192 |
theme=gr.themes.Soft(),
|
| 193 |
css=css,
|