Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
######################## UPDATED CODE #########################
|
| 3 |
+
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from typing import Dict, List
|
| 7 |
+
|
| 8 |
+
from mem0 import MemoryClient
|
| 9 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 10 |
+
from langchain.agents import create_tool_calling_agent, AgentExecutor
|
| 11 |
+
from langchain.chat_models import ChatOpenAI
|
| 12 |
+
import json
|
| 13 |
+
|
| 14 |
+
# Load config from local JSON file
|
| 15 |
+
with open("/content/drive/MyDrive/Generative AI/Project 3/config.json") as f:
|
| 16 |
+
config = json.load(f)
|
| 17 |
+
|
| 18 |
+
# Optional: If agentic_rag is in a separate file, import it
|
| 19 |
+
# from agentic_rag_workflow import agentic_rag
|
| 20 |
+
|
| 21 |
+
# ------------------------ Define NutritionBot ------------------------
|
| 22 |
+
class NutritionBot:
|
| 23 |
+
def __init__(self):
|
| 24 |
+
self.memory = MemoryClient(api_key=config.get("API_KEY"))
|
| 25 |
+
|
| 26 |
+
self.client = ChatOpenAI(
|
| 27 |
+
model_name="gpt-4o-mini",
|
| 28 |
+
api_key=config.get("API_KEY"),
|
| 29 |
+
endpoint=config.get("OPENAI_API_BASE"),
|
| 30 |
+
temperature=0
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
tools = [agentic_rag] # Make sure agentic_rag is defined or imported
|
| 34 |
+
|
| 35 |
+
system_prompt = """You are a caring and knowledgeable Medical Support Agent, specializing in nutrition disorder-related guidance. Your goal is to provide accurate, empathetic, and tailored nutritional recommendations while ensuring a seamless customer experience.
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 39 |
+
("system", system_prompt),
|
| 40 |
+
("human", "{input}"),
|
| 41 |
+
("placeholder", "{agent_scratchpad}")
|
| 42 |
+
])
|
| 43 |
+
|
| 44 |
+
agent = create_tool_calling_agent(self.client, tools, prompt)
|
| 45 |
+
self.agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
|
| 46 |
+
|
| 47 |
+
def store_customer_interaction(self, user_id: str, message: str, response: str, metadata: Dict = None):
|
| 48 |
+
if metadata is None:
|
| 49 |
+
metadata = {}
|
| 50 |
+
metadata["timestamp"] = datetime.now().isoformat()
|
| 51 |
+
conversation = [
|
| 52 |
+
{"role": "user", "content": message},
|
| 53 |
+
{"role": "assistant", "content": response}
|
| 54 |
+
]
|
| 55 |
+
self.memory.add(conversation, user_id=user_id, output_format="v1.1", metadata=metadata)
|
| 56 |
+
|
| 57 |
+
def get_relevant_history(self, user_id: str, query: str) -> List[Dict]:
|
| 58 |
+
return self.memory.search(query=query, user_id=user_id, limit=5)
|
| 59 |
+
|
| 60 |
+
def handle_customer_query(self, user_id: str, query: str) -> str:
|
| 61 |
+
relevant_history = self.get_relevant_history(user_id, query)
|
| 62 |
+
context = "Previous relevant interactions:\n"
|
| 63 |
+
for memory in relevant_history:
|
| 64 |
+
context += f"Customer: {memory['memory']}\n"
|
| 65 |
+
context += f"Support: {memory['memory']}\n"
|
| 66 |
+
context += "---\n"
|
| 67 |
+
|
| 68 |
+
prompt = f"""
|
| 69 |
+
Context:
|
| 70 |
+
{context}
|
| 71 |
+
Current customer query: {query}
|
| 72 |
+
Provide a helpful response that takes into account any relevant past interactions.
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
response = self.agent_executor.invoke({"input": prompt})
|
| 76 |
+
self.store_customer_interaction(user_id, query, response["output"], metadata={"type": "support_query"})
|
| 77 |
+
return response["output"]
|
| 78 |
+
|
| 79 |
+
# ------------------------ Gradio Interface ------------------------
|
| 80 |
+
bot = NutritionBot()
|
| 81 |
+
|
| 82 |
+
def respond(user_id, query):
|
| 83 |
+
return bot.handle_customer_query(user_id, query)
|
| 84 |
+
|
| 85 |
+
interface = gr.Interface(
|
| 86 |
+
fn=respond,
|
| 87 |
+
inputs=[
|
| 88 |
+
gr.Textbox(label="User ID", placeholder="Enter your name or ID"),
|
| 89 |
+
gr.Textbox(label="Query", placeholder="Ask about a nutrition disorder...")
|
| 90 |
+
],
|
| 91 |
+
outputs=gr.Textbox(label="Agent Response"),
|
| 92 |
+
title="Nutrition Disorder Specialist Agent",
|
| 93 |
+
description="Ask me anything about nutrition-related health issues, treatments, and recommendations!"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
interface.launch()
|