aslan-ng commited on
Commit
43021b3
·
verified ·
1 Parent(s): ce1933a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -60
app.py CHANGED
@@ -712,41 +712,10 @@ system_prompt = f"""
712
  {instruction}
713
  """
714
 
715
- client = openai.OpenAI(api_key=OPENAI_API)
716
-
717
- class MyOpenAIChatModel:
718
- def __init__(self, model_name: str = "gpt-4.1-mini"):
719
- self.model_name = model_name
720
-
721
- def __call__(self, messages, stop_sequences=None, **kwargs):
722
- """
723
- Adapter so smolagents' CodeAgent can call the OpenAI client.
724
-
725
- CodeAgent passes `messages` as a list of either:
726
- - dicts with 'role' and 'content'
727
- - or Message-like objects with .role and .content
728
- """
729
- normalized = []
730
- for m in messages:
731
- if isinstance(m, dict):
732
- normalized.append(m)
733
- else:
734
- role = getattr(m, "role", None)
735
- content = getattr(m, "content", None)
736
- if role is None or content is None:
737
- raise ValueError(
738
- f"Unexpected message type: {type(m)}; cannot extract role/content"
739
- )
740
- normalized.append({"role": role, "content": content})
741
-
742
- resp = client.chat.completions.create(
743
- model=self.model_name,
744
- messages=normalized,
745
- stop=stop_sequences,
746
- )
747
- return resp.choices[0].message.content
748
-
749
- model = MyOpenAIChatModel("gpt-4.1-mini")
750
 
751
  agent = smolagents.CodeAgent(
752
  tools=[
@@ -766,44 +735,53 @@ agent = smolagents.CodeAgent(
766
  max_steps=10,
767
  verbosity_level=2, # show steps in logs for class demo
768
  )
769
-
770
- def respond(message, history):
771
- try:
772
- out = str(agent.run(message))
773
- except Exception as e:
774
- out = f"[Error] {e}"
775
-
776
- history = (history or []) + [(message, out)]
777
- return "", history
778
 
779
- with gr.Blocks() as demo:
 
 
780
  gr.HTML("""
781
- <div style="text-align: center; font-family: 'Arial', sans-serif;">
782
- <h1 style="color:#1f77b4; margin-bottom: 20px; font-weight: 300;">
783
- 🤖 TechSpark AI Assistant
784
- </h1>
785
- <p style="margin-top: 0; font-weight: 300; font-size: 16px; color:#555;">
786
- Welcome to the TechSpark AI Assistant!<br>
787
- Ask anything about TechSpark staff, tools, courses or location of tools.<br>
788
- This assistant is powered by OpenAI's GPT model via smolagents,
789
- accessing accurate information from our curated dataset verified by TechSpark staff!
790
- </p>
791
- </div>
792
- """)
 
793
 
794
  chat = gr.Chatbot(height=420)
795
  inp = gr.Textbox(placeholder="Ask your question in natural language.", label="Your question")
796
 
797
- inp.submit(respond, [inp, chat], [inp, chat])
 
 
 
 
 
 
 
 
 
 
798
 
799
  gr.Examples(
800
  examples=[
801
  "Who is Ed?",
802
  "Who to talk to to create a wooden table?",
803
- "How can I access the laser cutters?",
804
- "How can I go to the welding space?"
805
  ],
806
  inputs=[inp],
 
 
 
807
  )
808
 
 
 
809
  demo.launch()
 
712
  {instruction}
713
  """
714
 
715
+ model = smolagents.OpenAIServerModel(
716
+ model_id="gpt-4.1-mini",
717
+ api_key=OPENAI_API,
718
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
719
 
720
  agent = smolagents.CodeAgent(
721
  tools=[
 
735
  max_steps=10,
736
  verbosity_level=2, # show steps in logs for class demo
737
  )
 
 
 
 
 
 
 
 
 
738
 
739
+ with gr.Blocks(theme=gr.themes.Ocean()) as demo:
740
+
741
+ # Centered title and description using HTML
742
  gr.HTML("""
743
+ <div style="text-align: center; font-family: 'Arial', sans-serif;">
744
+ <h1 style="color:#1f77b4; margin-bottom: 20px; font-weight: 300;">
745
+ 🤖 TechSpark AI Assistant
746
+ </h1>
747
+
748
+ <p style="margin-top: 0; font-weight: 300; font-size: 16px; color:#555;">
749
+ Welcome to the TechSpark AI Assistant!<br>
750
+ Ask anything about TechSpark staff, tools, courses or location of tools.<br>
751
+ This assistant is powered by OpenAI's GPT model via smolagents,
752
+ accessing accurate information from our curated dataset verified by TechSpark staff!
753
+ </p>
754
+ </div>
755
+ """)
756
 
757
  chat = gr.Chatbot(height=420)
758
  inp = gr.Textbox(placeholder="Ask your question in natural language.", label="Your question")
759
 
760
+ # No gr.State for agent just close over `agent`
761
+ def respond(message, history):
762
+ try:
763
+ # 1. Use agent.chat() to maintain internal history
764
+ out = str(agent.run(message))
765
+ except Exception as e:
766
+ out = f"[Error] {e}"
767
+
768
+ # This just updates the Gradio UI history
769
+ history = (history or []) + [(message, out)]
770
+ return "", history
771
 
772
  gr.Examples(
773
  examples=[
774
  "Who is Ed?",
775
  "Who to talk to to create a wooden table?",
776
+ "How to access the laser cutters?",
777
+ "How to go to the welding space?",
778
  ],
779
  inputs=[inp],
780
+ outputs=[inp, chat],
781
+ fn=respond,
782
+ cache_examples=False, # Set to False for dynamic content or to avoid caching issues
783
  )
784
 
785
+ inp.submit(respond, [inp, chat], [inp, chat])
786
+
787
  demo.launch()