Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,142 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from langchain_community.chat_models import ChatOpenAI
|
| 3 |
-
from langchain.memory import ConversationBufferMemory, SimpleMemory
|
| 4 |
-
from langchain.agents import initialize_agent, AgentType
|
| 5 |
-
from langchain_community.callbacks import ClearMLCallbackHandler
|
| 6 |
-
from langchain_core.callbacks import StdOutCallbackHandler
|
| 7 |
-
from clearml import Logger, Task
|
| 8 |
-
from dotenv import load_dotenv
|
| 9 |
-
from dotenv import load_dotenv, find_dotenv
|
| 10 |
-
import os
|
| 11 |
-
import agent.planning_agent as planning_agent
|
| 12 |
-
import logging
|
| 13 |
-
|
| 14 |
-
# Configure logging
|
| 15 |
-
logging.basicConfig(level=logging.INFO)
|
| 16 |
-
logger = logging.getLogger(__name__)
|
| 17 |
-
|
| 18 |
-
# Global variables
|
| 19 |
-
llm = None
|
| 20 |
-
chat_memory = None
|
| 21 |
-
query_memory = None
|
| 22 |
-
clearml_callback = None
|
| 23 |
-
|
| 24 |
-
def initialize_components():
|
| 25 |
-
global llm, chat_memory, query_memory, clearml_callback
|
| 26 |
-
load_dotenv()
|
| 27 |
-
|
| 28 |
-
OPENAI_API_KEY="sk-ah4Ar22m-1gdxwT5lg0KAodXVK5wt19cOzMBCV1mZ0T3BlbkFJzIhW66UefL-sl55mh4W-bcP1jUymqLHFuFEWC5bl8A"
|
| 29 |
-
CLEARML_API_ACCESS_KEY="L7F2UFV6OK9FN3XLK18F4Q166EK591F"
|
| 30 |
-
CLEARML_API_SECRET_KEY="L7zkP23yCOFFfFjX_21YV7ijyfe8a681YejTGF--f6Ovf98VCafY-84JAzwyiLphIBI"
|
| 31 |
-
SERPAPI_API_KEY="619f2302253fbe56448bcf82565caf2a3263d845944682533f10b09a0d1650e6"
|
| 32 |
-
|
| 33 |
-
# openai.api_key=OPENAI_API_KEY
|
| 34 |
-
|
| 35 |
-
llm = ChatOpenAI(
|
| 36 |
-
model_name="gpt-3.5-turbo",
|
| 37 |
-
temperature=0,
|
| 38 |
-
openai_api_key="sk-ah4Ar22m-1gdxwT5lg0KAodXVK5wt19cOzMBCV1mZ0T3BlbkFJzIhW66UefL-sl55mh4W-bcP1jUymqLHFuFEWC5bl8A"
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
# Initialize memories
|
| 42 |
-
chat_memory = ConversationBufferMemory(
|
| 43 |
-
memory_key="chat_history",
|
| 44 |
-
return_messages=True
|
| 45 |
-
)
|
| 46 |
-
query_memory = SimpleMemory()
|
| 47 |
-
|
| 48 |
-
# Setup and use the ClearML Callback
|
| 49 |
-
clearml_callback = ClearMLCallbackHandler(
|
| 50 |
-
task_type="inference",
|
| 51 |
-
project_name="langchain_callback_demo",
|
| 52 |
-
task_name="llm",
|
| 53 |
-
tags=["test"],
|
| 54 |
-
# Change the following parameters based on the amount of detail you want tracked
|
| 55 |
-
visualize=True,
|
| 56 |
-
complexity_metrics=True,
|
| 57 |
-
stream_logs=True,)
|
| 58 |
-
|
| 59 |
-
callbacks = [StdOutCallbackHandler(), clearml_callback]
|
| 60 |
-
|
| 61 |
-
# Initialize planning agent with both memories
|
| 62 |
-
planning_agent.initialize_planning_agent(llm, chat_memory, query_memory, callbacks)
|
| 63 |
-
|
| 64 |
-
logger.info("Components initialized successfully")
|
| 65 |
-
|
| 66 |
-
def process_query(query, history):
|
| 67 |
-
try:
|
| 68 |
-
# Restore chat history from Gradio's history
|
| 69 |
-
if history:
|
| 70 |
-
for human_msg, ai_msg in history:
|
| 71 |
-
if chat_memory and hasattr(chat_memory, 'chat_memory'):
|
| 72 |
-
chat_memory.chat_memory.add_user_message(human_msg)
|
| 73 |
-
chat_memory.chat_memory.add_ai_message(ai_msg)
|
| 74 |
-
|
| 75 |
-
# Store original query in query memory
|
| 76 |
-
query_memory.memories['original_query'] = query
|
| 77 |
-
|
| 78 |
-
# Execute query through planning agent
|
| 79 |
-
response = planning_agent.execute(query)
|
| 80 |
-
#clearml_callback.flush_tracker(name="Planning agent", finish=True)
|
| 81 |
-
#clearml_callback.flush_tracker(langchain_asset=llm, name="simple_sequential")
|
| 82 |
-
|
| 83 |
-
# Add current interaction to chat memory
|
| 84 |
-
if chat_memory and hasattr(chat_memory, 'chat_memory'):
|
| 85 |
-
chat_memory.chat_memory.add_user_message(query)
|
| 86 |
-
chat_memory.chat_memory.add_ai_message(response)
|
| 87 |
-
|
| 88 |
-
return response
|
| 89 |
-
|
| 90 |
-
except Exception as e:
|
| 91 |
-
error_msg = f"Error processing query: {str(e)}"
|
| 92 |
-
logger.error(f"Error details: {str(e)}")
|
| 93 |
-
|
| 94 |
-
if chat_memory and hasattr(chat_memory, 'chat_memory'):
|
| 95 |
-
chat_memory.chat_memory.add_user_message(query)
|
| 96 |
-
chat_memory.chat_memory.add_ai_message(error_msg)
|
| 97 |
-
|
| 98 |
-
return error_msg
|
| 99 |
-
|
| 100 |
-
def clear_context():
|
| 101 |
-
planning_agent.clear_context()
|
| 102 |
-
chat_memory.clear()
|
| 103 |
-
query_memory.memories.clear()
|
| 104 |
-
return [], []
|
| 105 |
-
|
| 106 |
-
def create_gradio_app():
|
| 107 |
-
from interface import create_interface
|
| 108 |
-
return create_interface(process_query, clear_context)
|
| 109 |
-
|
| 110 |
-
def report_table(loer, iteration=0):
|
| 111 |
-
# type: (Logger, int) -> ()
|
| 112 |
-
"""
|
| 113 |
-
reporting tables to the plots section
|
| 114 |
-
:param logger: The task.logger to use for sending the plots
|
| 115 |
-
:param iteration: The iteration number of the current reports
|
| 116 |
-
"""
|
| 117 |
-
# report table
|
| 118 |
-
# Report table - CSV from path
|
| 119 |
-
csv_path = './data/cleaned_dataset_full.csv'
|
| 120 |
-
loer.report_table("Data Set Capstone", "remote csv", iteration=iteration, csv=csv_path)
|
| 121 |
-
|
| 122 |
-
def main():
|
| 123 |
-
"""Main application entry point"""
|
| 124 |
-
try:
|
| 125 |
-
initialize_components()
|
| 126 |
-
app = create_gradio_app()
|
| 127 |
-
app.queue()
|
| 128 |
-
app.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
| 129 |
-
a_task = Task.get_task(project_name='langchain_callback_demo', task_name='llm')
|
| 130 |
-
loer = a_task.get_logger()
|
| 131 |
-
report_logs(loer)
|
| 132 |
-
# report text as debug example
|
| 133 |
-
report_debug_text(loer)
|
| 134 |
-
# report graphs
|
| 135 |
-
report_table(loer)
|
| 136 |
-
loer.flush()
|
| 137 |
-
except Exception as e:
|
| 138 |
-
logger.error(f"Error in main: {str(e)}")
|
| 139 |
-
raise
|
| 140 |
-
|
| 141 |
-
if __name__ == "__main__":
|
| 142 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|