Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,178 +1,6 @@
|
|
| 1 |
-
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from tools.perplexity_tools import (
|
| 4 |
-
get_ai_research_papers,
|
| 5 |
-
summarize_paper,
|
| 6 |
-
get_citation,
|
| 7 |
-
explain_concept,
|
| 8 |
-
)
|
| 9 |
-
from tools.general_tools import initialize_perplexity
|
| 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 handle chat UI interactions
|
| 16 |
-
def chat_ui_interaction(query: str, chat_history: list):
|
| 17 |
-
"""Handles user queries in the chat UI, showing the agent's thought process."""
|
| 18 |
-
global perplexity_api_key, perplexity_model
|
| 19 |
-
# Add user message to chat history
|
| 20 |
-
chat_history.append(("user", query))
|
| 21 |
-
|
| 22 |
-
# Let the agent decide which tool to invoke
|
| 23 |
-
thought_process = []
|
| 24 |
-
try:
|
| 25 |
-
# Simulate the agent's thought process
|
| 26 |
-
thought_process.append("🤔 Analyzing your query...")
|
| 27 |
-
|
| 28 |
-
# Example: Decide which tool to use based on the query
|
| 29 |
-
if "explain" in query.lower():
|
| 30 |
-
thought_process.append("🔍 Using the 'explain_concept' tool...")
|
| 31 |
-
result = explain_concept(query, perplexity_api_key, perplexity_model)
|
| 32 |
-
elif "summarize" in query.lower():
|
| 33 |
-
thought_process.append("📄 Using the 'summarize_paper' tool...")
|
| 34 |
-
result = summarize_paper(query, perplexity_api_key, perplexity_model)
|
| 35 |
-
elif "citation" in query.lower():
|
| 36 |
-
thought_process.append("📚 Using the 'get_citation' tool...")
|
| 37 |
-
result = get_citation(query, perplexity_api_key, perplexity_model)
|
| 38 |
-
else:
|
| 39 |
-
thought_process.append("🔎 Using the 'get_ai_research_papers' tool...")
|
| 40 |
-
result = get_ai_research_papers(query, perplexity_api_key, perplexity_model)
|
| 41 |
-
|
| 42 |
-
# Add thought process to chat history
|
| 43 |
-
for step in thought_process:
|
| 44 |
-
chat_history.append(("assistant", step))
|
| 45 |
-
|
| 46 |
-
# Add final result to chat history
|
| 47 |
-
chat_history.append(("assistant", result))
|
| 48 |
-
except Exception as e:
|
| 49 |
-
chat_history.append(("assistant", f"Error: {str(e)}"))
|
| 50 |
-
|
| 51 |
-
return chat_history
|
| 52 |
-
|
| 53 |
-
# Enhanced UI with toggle between current UI and chat UI
|
| 54 |
-
def create_ui():
|
| 55 |
-
with gr.Blocks() as demo:
|
| 56 |
-
# Toggle button to switch between UIs
|
| 57 |
-
toggle_button = gr.Radio(
|
| 58 |
-
choices=["Specific UI", "Agentic UI"],
|
| 59 |
-
label="Select UI Mode",
|
| 60 |
-
value="Specific UI"
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
# Specific UI components
|
| 64 |
-
with gr.Column(visible=True) as specific_ui:
|
| 65 |
-
gr.Markdown("# AI Research Assistant (Specific Mode)")
|
| 66 |
-
gr.Markdown("Your AI-powered research companion")
|
| 67 |
-
|
| 68 |
-
with gr.Row():
|
| 69 |
-
gr.Markdown("## Setup Required")
|
| 70 |
-
gr.Markdown("1. Enter Perplexity API key and select a model")
|
| 71 |
-
input_api_key = gr.Textbox(label="Enter your Perplexity API Key:")
|
| 72 |
-
model_dropdown = gr.Dropdown(
|
| 73 |
-
label="Select Perplexity Model",
|
| 74 |
-
choices=[
|
| 75 |
-
"sonar-reasoning-pro",
|
| 76 |
-
"sonar-reasoning",
|
| 77 |
-
"sonar-pro",
|
| 78 |
-
"sonar"
|
| 79 |
-
],
|
| 80 |
-
value="sonar-pro" # Default value
|
| 81 |
-
)
|
| 82 |
-
initialize_button = gr.Button("Initialize Perplexity API")
|
| 83 |
-
|
| 84 |
-
initialization_status = gr.Textbox(label="Initialization Status")
|
| 85 |
-
initialize_button.click(
|
| 86 |
-
fn=initialize_perplexity,
|
| 87 |
-
inputs=[input_api_key, model_dropdown],
|
| 88 |
-
outputs=initialization_status
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
with gr.Row():
|
| 92 |
-
gr.Markdown("## Research Tools")
|
| 93 |
-
input_query = gr.Textbox(label="Search query for AI papers:")
|
| 94 |
-
search_button = gr.Button("Search Papers")
|
| 95 |
-
|
| 96 |
-
search_results = gr.Textbox(label="Search Results")
|
| 97 |
-
search_button.click(
|
| 98 |
-
fn=get_ai_research_papers,
|
| 99 |
-
inputs=[input_query, input_api_key, model_dropdown],
|
| 100 |
-
outputs=search_results
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
with gr.Row():
|
| 104 |
-
input_paper_title = gr.Textbox(label="Paper title to summarize:")
|
| 105 |
-
summarize_button = gr.Button("Summarize Paper")
|
| 106 |
-
|
| 107 |
-
paper_summary = gr.Textbox(label="Paper Summary")
|
| 108 |
-
summarize_button.click(
|
| 109 |
-
fn=summarize_paper,
|
| 110 |
-
inputs=[input_paper_title, input_api_key, model_dropdown],
|
| 111 |
-
outputs=paper_summary
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
with gr.Row():
|
| 115 |
-
input_citation_title = gr.Textbox(label="Paper title to cite:")
|
| 116 |
-
citation_button = gr.Button("Generate Citation")
|
| 117 |
-
|
| 118 |
-
citation_output = gr.Textbox(label="Citation")
|
| 119 |
-
citation_button.click(
|
| 120 |
-
fn=get_citation,
|
| 121 |
-
inputs=[input_citation_title, input_api_key, model_dropdown],
|
| 122 |
-
outputs=citation_output
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
with gr.Row():
|
| 126 |
-
input_concept = gr.Textbox(label="Concept to explain:")
|
| 127 |
-
explain_button = gr.Button("Explain Concept")
|
| 128 |
-
|
| 129 |
-
explanation_output = gr.Textbox(label="Explanation")
|
| 130 |
-
explain_button.click(
|
| 131 |
-
fn=explain_concept,
|
| 132 |
-
inputs=[input_concept, input_api_key, model_dropdown],
|
| 133 |
-
outputs=explanation_output
|
| 134 |
-
)
|
| 135 |
-
|
| 136 |
-
# Agentic UI (Chat UI) components
|
| 137 |
-
with gr.Column(visible=False) as agentic_ui:
|
| 138 |
-
gr.Markdown("# AI Research Assistant (Agentic Mode)")
|
| 139 |
-
|
| 140 |
-
# Chat interface
|
| 141 |
-
with gr.Row():
|
| 142 |
-
# Left column for threads (optional)
|
| 143 |
-
with gr.Column(scale=1):
|
| 144 |
-
gr.Markdown("### Threads")
|
| 145 |
-
threads = gr.Textbox(label="Threads", interactive=False)
|
| 146 |
-
|
| 147 |
-
# Center column for chat messages
|
| 148 |
-
with gr.Column(scale=3):
|
| 149 |
-
chatbot = gr.Chatbot(label="Chat History")
|
| 150 |
-
chat_input = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
| 151 |
-
chat_button = gr.Button("Send")
|
| 152 |
-
|
| 153 |
-
# Handle chat interactions
|
| 154 |
-
chat_button.click(
|
| 155 |
-
fn=chat_ui_interaction,
|
| 156 |
-
inputs=[chat_input, chatbot],
|
| 157 |
-
outputs=chatbot
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
# Toggle between UIs
|
| 161 |
-
def toggle_ui(mode):
|
| 162 |
-
if mode == "Specific UI":
|
| 163 |
-
return [gr.Column.update(visible=True), gr.Column.update(visible=False)]
|
| 164 |
-
else:
|
| 165 |
-
return [gr.Column.update(visible=False), gr.Column.update(visible=True)]
|
| 166 |
-
|
| 167 |
-
toggle_button.change(
|
| 168 |
-
fn=toggle_ui,
|
| 169 |
-
inputs=toggle_button,
|
| 170 |
-
outputs=[specific_ui, agentic_ui]
|
| 171 |
-
)
|
| 172 |
-
|
| 173 |
-
return demo
|
| 174 |
|
| 175 |
# Launch the enhanced UI
|
| 176 |
if __name__ == "__main__":
|
| 177 |
-
demo =
|
| 178 |
demo.launch()
|
|
|
|
| 1 |
+
from main_ui import create_main_ui
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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