Spaces:
Sleeping
Sleeping
Create agentic_ui.py
Browse files- agentic_ui.py +70 -0
agentic_ui.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from tools.perplexity_tools import (
|
| 3 |
+
get_ai_research_papers,
|
| 4 |
+
summarize_paper,
|
| 5 |
+
get_citation,
|
| 6 |
+
explain_concept,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
def chat_ui_interaction(query: str, chat_history: list, api_key: str, model: str):
|
| 10 |
+
"""Handles user queries in the chat UI, showing the agent's thought process."""
|
| 11 |
+
# Add user message to chat history
|
| 12 |
+
chat_history.append(("user", query))
|
| 13 |
+
|
| 14 |
+
# Let the agent decide which tool to invoke
|
| 15 |
+
thought_process = []
|
| 16 |
+
try:
|
| 17 |
+
# Simulate the agent's thought process
|
| 18 |
+
thought_process.append("🤔 Analyzing your query...")
|
| 19 |
+
|
| 20 |
+
# Example: Decide which tool to use based on the query
|
| 21 |
+
if "explain" in query.lower():
|
| 22 |
+
thought_process.append("🔍 Using the 'explain_concept' tool...")
|
| 23 |
+
result = explain_concept(query, api_key, model)
|
| 24 |
+
elif "summarize" in query.lower():
|
| 25 |
+
thought_process.append("📄 Using the 'summarize_paper' tool...")
|
| 26 |
+
result = summarize_paper(query, api_key, model)
|
| 27 |
+
elif "citation" in query.lower():
|
| 28 |
+
thought_process.append("📚 Using the 'get_citation' tool...")
|
| 29 |
+
result = get_citation(query, api_key, model)
|
| 30 |
+
else:
|
| 31 |
+
thought_process.append("🔎 Using the 'get_ai_research_papers' tool...")
|
| 32 |
+
result = get_ai_research_papers(query, api_key, model)
|
| 33 |
+
|
| 34 |
+
# Add thought process to chat history
|
| 35 |
+
for step in thought_process:
|
| 36 |
+
chat_history.append(("assistant", step))
|
| 37 |
+
|
| 38 |
+
# Add final result to chat history
|
| 39 |
+
chat_history.append(("assistant", result))
|
| 40 |
+
except Exception as e:
|
| 41 |
+
chat_history.append(("assistant", f"Error: {str(e)}"))
|
| 42 |
+
|
| 43 |
+
return chat_history
|
| 44 |
+
|
| 45 |
+
def create_agentic_ui():
|
| 46 |
+
"""Creates the Agentic UI (Chat UI) components."""
|
| 47 |
+
with gr.Column() as agentic_ui:
|
| 48 |
+
gr.Markdown("# AI Research Assistant (Agentic Mode)")
|
| 49 |
+
|
| 50 |
+
# Chat interface
|
| 51 |
+
with gr.Row():
|
| 52 |
+
# Left column for threads (optional)
|
| 53 |
+
with gr.Column(scale=1):
|
| 54 |
+
gr.Markdown("### Threads")
|
| 55 |
+
threads = gr.Textbox(label="Threads", interactive=False)
|
| 56 |
+
|
| 57 |
+
# Center column for chat messages
|
| 58 |
+
with gr.Column(scale=3):
|
| 59 |
+
chatbot = gr.Chatbot(label="Chat History")
|
| 60 |
+
chat_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
| 61 |
+
chat_button = gr.Button("Send")
|
| 62 |
+
|
| 63 |
+
# Handle chat interactions
|
| 64 |
+
chat_button.click(
|
| 65 |
+
fn=chat_ui_interaction,
|
| 66 |
+
inputs=[chat_input, chatbot, input_api_key, model_dropdown],
|
| 67 |
+
outputs=chatbot
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
return agentic_ui
|