Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
-
# app.py (for Hugging Face Space & local dev)
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
from gradio.components import ChatMessage
|
|
|
|
|
|
|
| 5 |
import openai
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from langchain_openai import ChatOpenAI
|
|
@@ -10,13 +11,13 @@ from langchain_core.output_parsers import PydanticOutputParser
|
|
| 10 |
from langchain.agents import create_tool_calling_agent, AgentExecutor
|
| 11 |
from tools import save_tool
|
| 12 |
|
| 13 |
-
#
|
| 14 |
if os.getenv("HF_SPACE", None) is None:
|
| 15 |
from dotenv import load_dotenv
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
class ResearchResponse(BaseModel):
|
| 22 |
topic: str
|
|
@@ -27,80 +28,68 @@ class ResearchResponse(BaseModel):
|
|
| 27 |
sources: list[str]
|
| 28 |
tools_used: list[str]
|
| 29 |
|
| 30 |
-
|
|
|
|
| 31 |
parser = PydanticOutputParser(pydantic_object=ResearchResponse)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
tools = [
|
| 52 |
agent = create_tool_calling_agent(llm=llm, prompt=prompt, tools=tools)
|
| 53 |
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False)
|
| 54 |
|
| 55 |
-
# Gradio chat
|
| 56 |
-
|
| 57 |
def chat_fn(user_message, history):
|
| 58 |
-
#
|
| 59 |
history = history or []
|
| 60 |
-
#
|
| 61 |
history.append(ChatMessage(role="user", content=user_message))
|
| 62 |
|
| 63 |
-
|
|
|
|
| 64 |
"query": user_message,
|
| 65 |
-
"chat_history": [
|
|
|
|
|
|
|
| 66 |
})
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# return (what the interface shows as the last assistant message, full history)
|
| 76 |
-
return assistant_msg, history
|
| 77 |
-
|
| 78 |
-
css = ".chat-message.user {color: #0b5394;} .chat-message.bot {color: #38761d;}"
|
| 79 |
-
|
| 80 |
-
'''
|
| 81 |
-
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 82 |
-
# history is now a list of {"role":"user"/"assistant","content":...}
|
| 83 |
-
messages = [{"role":"system","content":system_message}] + history
|
| 84 |
-
messages.append({"role":"user","content":message})
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
messages.append({"role":"user","content":message})
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
model="gpt-4o-mini",
|
| 93 |
-
messages=messages,
|
| 94 |
-
stream=True
|
| 95 |
-
):
|
| 96 |
-
delta = chunk.choices[0].delta.get("content","")
|
| 97 |
-
resp += delta
|
| 98 |
-
yield resp'''
|
| 99 |
|
|
|
|
| 100 |
demo = gr.ChatInterface(
|
| 101 |
-
fn
|
| 102 |
-
title
|
| 103 |
-
type="messages"
|
| 104 |
)
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from gradio.components import ChatMessage
|
| 4 |
+
|
| 5 |
+
# — these are your LangChain imports —
|
| 6 |
import openai
|
| 7 |
from pydantic import BaseModel
|
| 8 |
from langchain_openai import ChatOpenAI
|
|
|
|
| 11 |
from langchain.agents import create_tool_calling_agent, AgentExecutor
|
| 12 |
from tools import save_tool
|
| 13 |
|
| 14 |
+
# load .env in dev
|
| 15 |
if os.getenv("HF_SPACE", None) is None:
|
| 16 |
from dotenv import load_dotenv
|
| 17 |
load_dotenv()
|
| 18 |
|
| 19 |
+
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
|
| 20 |
+
openai.api_key = OPENAI_API_KEY
|
| 21 |
|
| 22 |
class ResearchResponse(BaseModel):
|
| 23 |
topic: str
|
|
|
|
| 28 |
sources: list[str]
|
| 29 |
tools_used: list[str]
|
| 30 |
|
| 31 |
+
# 1) set up LLM + parser
|
| 32 |
+
llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, model="gpt-4o-mini")
|
| 33 |
parser = PydanticOutputParser(pydantic_object=ResearchResponse)
|
| 34 |
|
| 35 |
+
# 2) your custom prompt
|
| 36 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 37 |
+
("system", (
|
| 38 |
+
"You are an empathetic, culturally and religiously sensitive AI assistant "
|
| 39 |
+
"specializing in mental health support for Muslim women. Always respond:\n"
|
| 40 |
+
"1. Acknowledge the user’s feelings.\n"
|
| 41 |
+
"2. Validate gently.\n"
|
| 42 |
+
"3. Offer a concise informative summary.\n"
|
| 43 |
+
"4. End with a short open‐ended question.\n"
|
| 44 |
+
"Optionally cite one Quranic verse/tafsir from Al-Mizan.\n"
|
| 45 |
+
"Return **only** valid JSON matching the `ResearchResponse` schema:\n"
|
| 46 |
+
"{format_instructions}"
|
| 47 |
+
)),
|
| 48 |
+
("human", "{query}"),
|
| 49 |
+
])
|
| 50 |
+
prompt = prompt.partial(format_instructions=parser.get_format_instructions())
|
| 51 |
+
|
| 52 |
+
# 3) wire up your tools + agent
|
| 53 |
+
tools = []
|
| 54 |
agent = create_tool_calling_agent(llm=llm, prompt=prompt, tools=tools)
|
| 55 |
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False)
|
| 56 |
|
| 57 |
+
# 4) the Gradio chat function
|
|
|
|
| 58 |
def chat_fn(user_message, history):
|
| 59 |
+
# history is List[ChatMessage], or None
|
| 60 |
history = history or []
|
| 61 |
+
# append the user turn as a ChatMessage
|
| 62 |
history.append(ChatMessage(role="user", content=user_message))
|
| 63 |
|
| 64 |
+
# invoke the agent; we pass along the raw chat history as dicts if you need them
|
| 65 |
+
agent_output = agent_executor.invoke({
|
| 66 |
"query": user_message,
|
| 67 |
+
"chat_history": [
|
| 68 |
+
{"role": m.role, "content": m.content} for m in history
|
| 69 |
+
]
|
| 70 |
})
|
| 71 |
+
# parse its JSON output
|
| 72 |
+
out = parser.parse(agent_output["output"])
|
| 73 |
|
| 74 |
+
# build the assistant’s final string
|
| 75 |
+
assistant_content = "\n".join([
|
| 76 |
+
out.empathetic_response,
|
| 77 |
+
out.informative_response,
|
| 78 |
+
out.quran,
|
| 79 |
+
out.question,
|
| 80 |
+
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
# append as ChatMessage
|
| 83 |
+
history.append(ChatMessage(role="assistant", content=assistant_content))
|
|
|
|
| 84 |
|
| 85 |
+
# return last assistant message (so the UI updates), and the full history
|
| 86 |
+
return assistant_content, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# 5) launch ChatInterface
|
| 89 |
demo = gr.ChatInterface(
|
| 90 |
+
fn=chat_fn,
|
| 91 |
+
title="Yaqin",
|
| 92 |
+
type="messages", # ensures we expect ChatMessage objects
|
| 93 |
)
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|