Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,19 +6,11 @@ from tools.perplexity_tools import (
|
|
| 6 |
explain_concept,
|
| 7 |
)
|
| 8 |
from tools.general_tools import initialize_perplexity
|
| 9 |
-
import time
|
| 10 |
|
| 11 |
# Initialize Perplexity without API key and model
|
| 12 |
perplexity_api_key = None
|
| 13 |
perplexity_model = "sonar-pro" # Default model
|
| 14 |
|
| 15 |
-
# Function to simulate streaming
|
| 16 |
-
def simulate_streaming(text: str, delay: float = 0.05):
|
| 17 |
-
"""Simulates streaming by yielding text incrementally."""
|
| 18 |
-
for char in text:
|
| 19 |
-
time.sleep(delay)
|
| 20 |
-
yield char
|
| 21 |
-
|
| 22 |
# Function to handle chat UI interactions
|
| 23 |
def chat_ui_interaction(query: str, chat_history: list):
|
| 24 |
"""Handles user queries in the chat UI, showing the agent's thought process."""
|
|
@@ -26,52 +18,50 @@ def chat_ui_interaction(query: str, chat_history: list):
|
|
| 26 |
# Add user message to chat history
|
| 27 |
chat_history.append(("user", query))
|
| 28 |
|
| 29 |
-
# Let the agent decide which
|
| 30 |
thought_process = []
|
| 31 |
try:
|
| 32 |
# Simulate the agent's thought process
|
| 33 |
thought_process.append("🤔 Analyzing your query...")
|
| 34 |
-
yield chat_history + [("assistant", "🤔 Analyzing your query...")]
|
| 35 |
|
| 36 |
# Example: Decide which tool to use based on the query
|
| 37 |
if "explain" in query.lower():
|
| 38 |
thought_process.append("🔍 Using the 'explain_concept' tool...")
|
| 39 |
-
yield chat_history + [("assistant", "🔍 Using the 'explain_concept' tool...")]
|
| 40 |
result = explain_concept(query, perplexity_api_key, perplexity_model)
|
| 41 |
elif "summarize" in query.lower():
|
| 42 |
thought_process.append("📄 Using the 'summarize_paper' tool...")
|
| 43 |
-
yield chat_history + [("assistant", "📄 Using the 'summarize_paper' tool...")]
|
| 44 |
result = summarize_paper(query, perplexity_api_key, perplexity_model)
|
| 45 |
elif "citation" in query.lower():
|
| 46 |
thought_process.append("📚 Using the 'get_citation' tool...")
|
| 47 |
-
yield chat_history + [("assistant", "📚 Using the 'get_citation' tool...")]
|
| 48 |
result = get_citation(query, perplexity_api_key, perplexity_model)
|
| 49 |
else:
|
| 50 |
thought_process.append("🔎 Using the 'get_ai_research_papers' tool...")
|
| 51 |
-
yield chat_history + [("assistant", "🔎 Using the 'get_ai_research_papers' tool...")]
|
| 52 |
result = get_ai_research_papers(query, perplexity_api_key, perplexity_model)
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
for
|
| 56 |
-
chat_history.append(("assistant",
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
except Exception as e:
|
| 59 |
chat_history.append(("assistant", f"Error: {str(e)}"))
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
# Enhanced UI with toggle between current UI and chat UI
|
| 63 |
def create_ui():
|
| 64 |
with gr.Blocks() as demo:
|
| 65 |
# Toggle button to switch between UIs
|
| 66 |
toggle_button = gr.Radio(
|
| 67 |
-
choices=["
|
| 68 |
label="Select UI Mode",
|
| 69 |
-
value="
|
| 70 |
)
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
with gr.Column(visible=True) as
|
| 74 |
-
gr.Markdown("# AI Research Assistant")
|
| 75 |
gr.Markdown("Your AI-powered research companion")
|
| 76 |
|
| 77 |
with gr.Row():
|
|
@@ -142,22 +132,18 @@ def create_ui():
|
|
| 142 |
outputs=explanation_output
|
| 143 |
)
|
| 144 |
|
| 145 |
-
# Chat UI components
|
| 146 |
-
with gr.Column(visible=False) as
|
| 147 |
-
gr.Markdown("# AI Research Assistant (
|
| 148 |
|
| 149 |
-
# Chat
|
| 150 |
with gr.Row():
|
| 151 |
-
# Left
|
| 152 |
with gr.Column(scale=1):
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
value=[["1", "AI Research"], ["2", "Paper Summaries"]],
|
| 156 |
-
interactive=False,
|
| 157 |
-
label="Threads"
|
| 158 |
-
)
|
| 159 |
|
| 160 |
-
# Center
|
| 161 |
with gr.Column(scale=3):
|
| 162 |
chatbot = gr.Chatbot(label="Chat History")
|
| 163 |
chat_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
|
@@ -172,7 +158,7 @@ def create_ui():
|
|
| 172 |
|
| 173 |
# Toggle between UIs
|
| 174 |
def toggle_ui(mode):
|
| 175 |
-
if mode == "
|
| 176 |
return [gr.Column.update(visible=True), gr.Column.update(visible=False)]
|
| 177 |
else:
|
| 178 |
return [gr.Column.update(visible=False), gr.Column.update(visible=True)]
|
|
@@ -180,7 +166,7 @@ def create_ui():
|
|
| 180 |
toggle_button.change(
|
| 181 |
fn=toggle_ui,
|
| 182 |
inputs=toggle_button,
|
| 183 |
-
outputs=[
|
| 184 |
)
|
| 185 |
|
| 186 |
return demo
|
|
|
|
| 6 |
explain_concept,
|
| 7 |
)
|
| 8 |
from tools.general_tools import initialize_perplexity
|
|
|
|
| 9 |
|
| 10 |
# Initialize Perplexity without API key and model
|
| 11 |
perplexity_api_key = None
|
| 12 |
perplexity_model = "sonar-pro" # Default model
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# Function to handle chat UI interactions
|
| 15 |
def chat_ui_interaction(query: str, chat_history: list):
|
| 16 |
"""Handles user queries in the chat UI, showing the agent's thought process."""
|
|
|
|
| 18 |
# Add user message to chat history
|
| 19 |
chat_history.append(("user", query))
|
| 20 |
|
| 21 |
+
# Let the agent decide which tool to invoke
|
| 22 |
thought_process = []
|
| 23 |
try:
|
| 24 |
# Simulate the agent's thought process
|
| 25 |
thought_process.append("🤔 Analyzing your query...")
|
|
|
|
| 26 |
|
| 27 |
# Example: Decide which tool to use based on the query
|
| 28 |
if "explain" in query.lower():
|
| 29 |
thought_process.append("🔍 Using the 'explain_concept' tool...")
|
|
|
|
| 30 |
result = explain_concept(query, perplexity_api_key, perplexity_model)
|
| 31 |
elif "summarize" in query.lower():
|
| 32 |
thought_process.append("📄 Using the 'summarize_paper' tool...")
|
|
|
|
| 33 |
result = summarize_paper(query, perplexity_api_key, perplexity_model)
|
| 34 |
elif "citation" in query.lower():
|
| 35 |
thought_process.append("📚 Using the 'get_citation' tool...")
|
|
|
|
| 36 |
result = get_citation(query, perplexity_api_key, perplexity_model)
|
| 37 |
else:
|
| 38 |
thought_process.append("🔎 Using the 'get_ai_research_papers' tool...")
|
|
|
|
| 39 |
result = get_ai_research_papers(query, perplexity_api_key, perplexity_model)
|
| 40 |
|
| 41 |
+
# Add thought process to chat history
|
| 42 |
+
for step in thought_process:
|
| 43 |
+
chat_history.append(("assistant", step))
|
| 44 |
+
|
| 45 |
+
# Add final result to chat history
|
| 46 |
+
chat_history.append(("assistant", result))
|
| 47 |
except Exception as e:
|
| 48 |
chat_history.append(("assistant", f"Error: {str(e)}"))
|
| 49 |
+
|
| 50 |
+
return chat_history
|
| 51 |
|
| 52 |
# Enhanced UI with toggle between current UI and chat UI
|
| 53 |
def create_ui():
|
| 54 |
with gr.Blocks() as demo:
|
| 55 |
# Toggle button to switch between UIs
|
| 56 |
toggle_button = gr.Radio(
|
| 57 |
+
choices=["Specific UI", "Agentic UI"],
|
| 58 |
label="Select UI Mode",
|
| 59 |
+
value="Specific UI"
|
| 60 |
)
|
| 61 |
|
| 62 |
+
# Specific UI components
|
| 63 |
+
with gr.Column(visible=True) as specific_ui:
|
| 64 |
+
gr.Markdown("# AI Research Assistant (Specific Mode)")
|
| 65 |
gr.Markdown("Your AI-powered research companion")
|
| 66 |
|
| 67 |
with gr.Row():
|
|
|
|
| 132 |
outputs=explanation_output
|
| 133 |
)
|
| 134 |
|
| 135 |
+
# Agentic UI (Chat UI) components
|
| 136 |
+
with gr.Column(visible=False) as agentic_ui:
|
| 137 |
+
gr.Markdown("# AI Research Assistant (Agentic Mode)")
|
| 138 |
|
| 139 |
+
# Chat interface
|
| 140 |
with gr.Row():
|
| 141 |
+
# Left column for threads (optional)
|
| 142 |
with gr.Column(scale=1):
|
| 143 |
+
gr.Markdown("### Threads")
|
| 144 |
+
threads = gr.Textbox(label="Threads", interactive=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
+
# Center column for chat messages
|
| 147 |
with gr.Column(scale=3):
|
| 148 |
chatbot = gr.Chatbot(label="Chat History")
|
| 149 |
chat_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
|
|
|
| 158 |
|
| 159 |
# Toggle between UIs
|
| 160 |
def toggle_ui(mode):
|
| 161 |
+
if mode == "Specific UI":
|
| 162 |
return [gr.Column.update(visible=True), gr.Column.update(visible=False)]
|
| 163 |
else:
|
| 164 |
return [gr.Column.update(visible=False), gr.Column.update(visible=True)]
|
|
|
|
| 166 |
toggle_button.change(
|
| 167 |
fn=toggle_ui,
|
| 168 |
inputs=toggle_button,
|
| 169 |
+
outputs=[specific_ui, agentic_ui]
|
| 170 |
)
|
| 171 |
|
| 172 |
return demo
|