Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,6 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import re
|
| 4 |
from gradio.components import ChatMessage
|
| 5 |
-
from huggingface_hub import InferenceClient
|
| 6 |
-
|
| 7 |
-
|
| 8 |
import openai
|
| 9 |
from pydantic import BaseModel
|
| 10 |
from langchain_openai import ChatOpenAI
|
|
@@ -18,9 +14,11 @@ if os.getenv("HF_SPACE", None) is None:
|
|
| 18 |
from dotenv import load_dotenv
|
| 19 |
load_dotenv()
|
| 20 |
|
|
|
|
| 21 |
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
|
| 22 |
openai.api_key = OPENAI_API_KEY
|
| 23 |
|
|
|
|
| 24 |
class ResearchResponse(BaseModel):
|
| 25 |
topic: str
|
| 26 |
empathetic_response: str
|
|
@@ -30,11 +28,11 @@ class ResearchResponse(BaseModel):
|
|
| 30 |
sources: list[str]
|
| 31 |
tools_used: list[str]
|
| 32 |
|
| 33 |
-
#
|
| 34 |
llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, model="gpt-4o-mini")
|
| 35 |
parser = PydanticOutputParser(pydantic_object=ResearchResponse)
|
| 36 |
|
| 37 |
-
#
|
| 38 |
prompt = ChatPromptTemplate.from_messages([
|
| 39 |
(
|
| 40 |
"system",
|
|
@@ -61,20 +59,19 @@ prompt = ChatPromptTemplate.from_messages([
|
|
| 61 |
])
|
| 62 |
prompt = prompt.partial(format_instructions=parser.get_format_instructions())
|
| 63 |
|
| 64 |
-
#
|
| 65 |
tools = [save_tool]
|
| 66 |
agent = create_tool_calling_agent(llm=llm, prompt=prompt, tools=tools)
|
| 67 |
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False)
|
| 68 |
|
| 69 |
-
# 4) the Gradio chat function
|
| 70 |
|
| 71 |
-
|
| 72 |
|
| 73 |
def respond(message, history: list[dict]):
|
| 74 |
|
| 75 |
agent_output = agent_executor.invoke({
|
| 76 |
"query": message,
|
| 77 |
-
"chat_history": history
|
| 78 |
})
|
| 79 |
|
| 80 |
raw = agent_output["output"]
|
|
@@ -89,7 +86,6 @@ def respond(message, history: list[dict]):
|
|
| 89 |
out.question,
|
| 90 |
])
|
| 91 |
except Exception as e:
|
| 92 |
-
# Fallback: just use the raw text if JSON parsing fails
|
| 93 |
print("Fallback to exception: raw")
|
| 94 |
assistant_text = raw
|
| 95 |
|
|
@@ -98,13 +94,13 @@ def respond(message, history: list[dict]):
|
|
| 98 |
yield response
|
| 99 |
|
| 100 |
|
| 101 |
-
#
|
| 102 |
demo = gr.ChatInterface(
|
| 103 |
respond,
|
| 104 |
title="YAQIN Chatbot",
|
| 105 |
description="Culturally Sensitive Chatbot for Muslim Women Wanting Mental Healthcare",
|
| 106 |
-
type="messages",
|
| 107 |
)
|
| 108 |
|
| 109 |
if __name__ == "__main__":
|
| 110 |
-
demo.launch(
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from gradio.components import ChatMessage
|
|
|
|
|
|
|
|
|
|
| 4 |
import openai
|
| 5 |
from pydantic import BaseModel
|
| 6 |
from langchain_openai import ChatOpenAI
|
|
|
|
| 14 |
from dotenv import load_dotenv
|
| 15 |
load_dotenv()
|
| 16 |
|
| 17 |
+
# API Keys
|
| 18 |
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]
|
| 19 |
openai.api_key = OPENAI_API_KEY
|
| 20 |
|
| 21 |
+
# response structure
|
| 22 |
class ResearchResponse(BaseModel):
|
| 23 |
topic: str
|
| 24 |
empathetic_response: str
|
|
|
|
| 28 |
sources: list[str]
|
| 29 |
tools_used: list[str]
|
| 30 |
|
| 31 |
+
# set up LLM and parser
|
| 32 |
llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, model="gpt-4o-mini")
|
| 33 |
parser = PydanticOutputParser(pydantic_object=ResearchResponse)
|
| 34 |
|
| 35 |
+
# custom prompt to API
|
| 36 |
prompt = ChatPromptTemplate.from_messages([
|
| 37 |
(
|
| 38 |
"system",
|
|
|
|
| 59 |
])
|
| 60 |
prompt = prompt.partial(format_instructions=parser.get_format_instructions())
|
| 61 |
|
| 62 |
+
# set up tool and agent
|
| 63 |
tools = [save_tool]
|
| 64 |
agent = create_tool_calling_agent(llm=llm, prompt=prompt, tools=tools)
|
| 65 |
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=False)
|
| 66 |
|
|
|
|
| 67 |
|
| 68 |
+
# Gradio chat response function
|
| 69 |
|
| 70 |
def respond(message, history: list[dict]):
|
| 71 |
|
| 72 |
agent_output = agent_executor.invoke({
|
| 73 |
"query": message,
|
| 74 |
+
"chat_history": history
|
| 75 |
})
|
| 76 |
|
| 77 |
raw = agent_output["output"]
|
|
|
|
| 86 |
out.question,
|
| 87 |
])
|
| 88 |
except Exception as e:
|
|
|
|
| 89 |
print("Fallback to exception: raw")
|
| 90 |
assistant_text = raw
|
| 91 |
|
|
|
|
| 94 |
yield response
|
| 95 |
|
| 96 |
|
| 97 |
+
# launch ChatInterface
|
| 98 |
demo = gr.ChatInterface(
|
| 99 |
respond,
|
| 100 |
title="YAQIN Chatbot",
|
| 101 |
description="Culturally Sensitive Chatbot for Muslim Women Wanting Mental Healthcare",
|
| 102 |
+
type="messages",
|
| 103 |
)
|
| 104 |
|
| 105 |
if __name__ == "__main__":
|
| 106 |
+
demo.launch()
|