Spaces:
YZ03
/
Runtime error

YZ03 commited on
Commit
a8e953d
·
verified ·
1 Parent(s): 8b1269f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -62
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
- # Load local .env only in development; in Spaces rely on HF Secrets
14
  if os.getenv("HF_SPACE", None) is None:
15
  from dotenv import load_dotenv
16
  load_dotenv()
17
 
18
- # Retrieve your OpenAI API key from environment (HF Secret or .env)
19
- openai_api_key = os.environ["OPENAI_API_KEY"]
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
- llm = ChatOpenAI(openai_api_key=openai_api_key, model="gpt-4.1-nano-2025-04-14")
 
31
  parser = PydanticOutputParser(pydantic_object=ResearchResponse)
32
 
33
- prompt = ChatPromptTemplate.from_messages(
34
- [
35
- ("system", (
36
- "You are an empathetic, culturally and religiously sensitive AI assistant specializing in mental health support for Muslim women. "
37
- "Your responses must always:\n"
38
- "1. Start with a kind acknowledgment of the user's feelings or situation.\n"
39
- "2. Offer a gentle validation.\n"
40
- "3. Provide a brief, informative summary.\n"
41
- "4. Ask a short, open-ended reflection question.\n"
42
- "Optionally, include a Quranic verse/tafsir snippet from AlMizan if relevant.\n"
43
- "After thinking, return JSON matching the ResearchResponse schema. Don’t output other text.\n{format_instructions}"
44
- )),
45
- ("placeholder", "{chat_history}"),
46
- ("human", "{query}"),
47
- ("placeholder", "{agent_scratchpad}"),
48
- ]
49
- ).partial(format_instructions=parser.get_format_instructions())
50
-
51
- tools = [save_tool]
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 interface
56
-
57
  def chat_fn(user_message, history):
58
- # make sure history is a list of ChatMessage
59
  history = history or []
60
- # add the user turn
61
  history.append(ChatMessage(role="user", content=user_message))
62
 
63
- result = agent_executor.invoke({
 
64
  "query": user_message,
65
- "chat_history": [ {"role": m.role, "content": m.content} for m in history ]
 
 
66
  })
 
 
67
 
68
- # 3) parse the JSON string back into your schema
69
- out = parser.parse(result["output"])
70
-
71
- assistant_msg = f"{out.empathetic_response}\n{out.informative_response}\n{out.quran}\n{out.question}"
72
- # add the assistant turn
73
- history.append(ChatMessage(role="assistant", content=assistant_msg))
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
- for u,b in history:
87
- msgs += [{"role":"user","content":u}, {"role":"assistant","content":b}]
88
- messages.append({"role":"user","content":message})
89
 
90
- resp = ""
91
- for chunk in openai.chat.completions.create(
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 = chat_fn,
102
- title = "Yaqin",
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 users feelings.\n"
41
+ "2. Validate gently.\n"
42
+ "3. Offer a concise informative summary.\n"
43
+ "4. End with a short openended 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__":