Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -521,140 +521,35 @@ agent = smolagents.CodeAgent(
|
|
| 521 |
# --- Page config ---
|
| 522 |
st.set_page_config(page_title="TechSpark AI Assistant", layout="wide")
|
| 523 |
|
| 524 |
-
# --- Sidebar
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
|
| 540 |
-
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
|
| 544 |
-
|
| 545 |
-
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
section[data-testid="stSidebar"] * {
|
| 555 |
-
font-size: .8 vw !important;
|
| 556 |
-
}
|
| 557 |
-
|
| 558 |
-
/* --- TITLES (untouched) --- */
|
| 559 |
-
|
| 560 |
-
/* --- MASSIVE CHAT BUBBLES --- */
|
| 561 |
-
div[data-testid="chat-message"] {
|
| 562 |
-
font-size: 5 vw !important; /* HUGE readable text */
|
| 563 |
-
line-height: 2!important;
|
| 564 |
-
padding: 2vw 2.5vw !important; /* large padding */
|
| 565 |
-
border-radius: 2vw !important;
|
| 566 |
-
max-width: 70% !important;
|
| 567 |
-
}
|
| 568 |
-
|
| 569 |
-
/* USER MESSAGE */
|
| 570 |
-
div[data-testid="chat-message-user"] {
|
| 571 |
-
margin-left: auto !important;
|
| 572 |
-
background: #00796b !important;
|
| 573 |
-
color: white !important;
|
| 574 |
-
}
|
| 575 |
-
|
| 576 |
-
/* ASSISTANT MESSAGE */
|
| 577 |
-
div[data-testid="chat-message-assistant"] {
|
| 578 |
-
margin-right: auto !important;
|
| 579 |
-
background: #222 !important;
|
| 580 |
-
color: white !important;
|
| 581 |
-
}
|
| 582 |
-
|
| 583 |
-
/* --- INPUT BOX --- */
|
| 584 |
-
.stTextInput textarea {
|
| 585 |
-
font-size: 2 vw !important;
|
| 586 |
-
padding: 1.4vw !important;
|
| 587 |
-
min-height: 8vh !important;
|
| 588 |
-
border-radius: 1.5vw !important;
|
| 589 |
-
}
|
| 590 |
-
|
| 591 |
-
/* --- SEND BUTTON --- */
|
| 592 |
-
.stButton > button {
|
| 593 |
-
font-size: 2 vw !important;
|
| 594 |
-
padding: 1vw 2vw !important;
|
| 595 |
-
border-radius: 1.5vw !important;
|
| 596 |
-
}
|
| 597 |
-
|
| 598 |
-
|
| 599 |
-
#--SCALE---
|
| 600 |
-
|
| 601 |
-
/* Global scale to simulate 120% zoom */
|
| 602 |
-
html {
|
| 603 |
-
transform: scale(1.2);
|
| 604 |
-
transform-origin: top center;
|
| 605 |
-
}
|
| 606 |
-
|
| 607 |
-
/* Prevent horizontal scrollbar after scaling */
|
| 608 |
-
body, .stApp {
|
| 609 |
-
width: 83.33%; /* 1 / 1.2 */
|
| 610 |
-
margin: 0 auto;
|
| 611 |
-
}
|
| 612 |
-
|
| 613 |
-
</style>
|
| 614 |
-
""", unsafe_allow_html=True)
|
| 615 |
-
|
| 616 |
-
|
| 617 |
-
# --- Centered main title ---
|
| 618 |
-
|
| 619 |
-
st.markdown("<h1 class='main-title' style='text-align:center;'>TechSpark AI Assistant</h1>", unsafe_allow_html=True)
|
| 620 |
-
st.markdown("<h2 class='sub-title' style='text-align:center;'>Ask me anything about TechSpark — </h2>", unsafe_allow_html=True)
|
| 621 |
-
|
| 622 |
-
|
| 623 |
-
# --- Initialize chat history ---
|
| 624 |
-
if 'generated' not in st.session_state:
|
| 625 |
-
st.session_state['generated'] = ["Hi! I'm your AI assistant. How can I help you today?"]
|
| 626 |
-
if 'past' not in st.session_state:
|
| 627 |
-
st.session_state['past'] = ["Hi!"]
|
| 628 |
-
|
| 629 |
-
# --- Layout containers ---
|
| 630 |
-
input_container = st.container()
|
| 631 |
-
colored_header(label='', description='', color_name='blue-30')
|
| 632 |
-
response_container = st.container()
|
| 633 |
-
|
| 634 |
-
# --- User input ---
|
| 635 |
-
def get_text():
|
| 636 |
-
input_text = st.text_input("You:", "", key="input", placeholder="Type your message here...")
|
| 637 |
-
return input_text
|
| 638 |
-
|
| 639 |
-
with input_container:
|
| 640 |
-
user_input = get_text()
|
| 641 |
-
|
| 642 |
-
# --- Generate AI response ---
|
| 643 |
-
def generate_response(prompt):
|
| 644 |
-
try:
|
| 645 |
-
return str(smolagents.agent(prompt))
|
| 646 |
-
except Exception as e:
|
| 647 |
-
return f"[Error] {e}"
|
| 648 |
-
|
| 649 |
-
# --- Display responses ---
|
| 650 |
-
with response_container:
|
| 651 |
-
if user_input:
|
| 652 |
-
response = generate_response(user_input)
|
| 653 |
-
st.session_state.past.append(user_input)
|
| 654 |
-
st.session_state.generated.append(response)
|
| 655 |
-
|
| 656 |
-
if st.session_state['generated']:
|
| 657 |
-
for i in range(len(st.session_state['generated'])):
|
| 658 |
-
message(st.session_state['past'][i], is_user=True, key=f"{i}_user")
|
| 659 |
-
message(st.session_state['generated'][i], key=f"{i}_assistant")
|
| 660 |
|
|
|
|
|
|
| 521 |
# --- Page config ---
|
| 522 |
st.set_page_config(page_title="TechSpark AI Assistant", layout="wide")
|
| 523 |
|
| 524 |
+
# --- Sidebar --
|
| 525 |
+
import gradio as gr
|
| 526 |
+
|
| 527 |
+
# Minimal Gradio chat
|
| 528 |
+
with gr.Blocks(title="TechSpark Agent") as demo:
|
| 529 |
+
gr.Markdown("## Beam Agent — Custom Tool Selection (smolagents + llama.cpp)")
|
| 530 |
+
chat = gr.Chatbot(height=420)
|
| 531 |
+
inp = gr.Textbox(placeholder="Ask your question in natural language.", label="Your question")
|
| 532 |
+
|
| 533 |
+
# No gr.State for agent — just close over `agent`
|
| 534 |
+
def respond(message, history):
|
| 535 |
+
try:
|
| 536 |
+
# 1. Use agent.chat() to maintain internal history
|
| 537 |
+
out = str(agent.run(message))
|
| 538 |
+
except Exception as e:
|
| 539 |
+
out = f"[Error] {e}"
|
| 540 |
+
|
| 541 |
+
# This just updates the Gradio UI history
|
| 542 |
+
history = (history or []) + [(message, out)]
|
| 543 |
+
return "", history
|
| 544 |
+
|
| 545 |
+
gr.Examples(
|
| 546 |
+
fn=respond,
|
| 547 |
+
examples=[
|
| 548 |
+
"Who is Ed?",
|
| 549 |
+
"Who to talk to to create a wooden table?",
|
| 550 |
+
"how to access laser cutter"
|
| 551 |
+
],
|
| 552 |
+
inputs=[inp]
|
| 553 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 554 |
|
| 555 |
+
inp.submit(respond, [inp, chat], [inp, chat])
|