Spaces:
Running
Running
Updated conversation history to be a session state
Browse files
app.py
CHANGED
@@ -4,10 +4,7 @@ import os
|
|
4 |
|
5 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
def get_chatbot_response(user_message, country, language):
|
10 |
-
global conversation_history
|
11 |
|
12 |
system_message = (
|
13 |
f"You are a lawyer specializing in providing concise and accurate legal information based on the laws in {country}. "
|
@@ -40,7 +37,12 @@ def get_chatbot_response(user_message, country, language):
|
|
40 |
|
41 |
conversation_history.append({"role": "assistant", "content": response})
|
42 |
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
theme = gr.themes.Ocean(
|
46 |
text_size="lg",
|
@@ -83,8 +85,6 @@ custom_css = """
|
|
83 |
"""
|
84 |
|
85 |
def clear_history():
|
86 |
-
global conversation_history
|
87 |
-
conversation_history = []
|
88 |
return []
|
89 |
|
90 |
|
@@ -106,8 +106,9 @@ with gr.Blocks(theme = theme, css = custom_css) as demo:
|
|
106 |
|
107 |
custom_country_input = gr.Textbox(label="Enter Country (if not listed)", visible=False)
|
108 |
|
109 |
-
chatbot = gr.Chatbot(label="π¬ Chat History")
|
110 |
-
chatbot.clear(fn=clear_history, outputs=
|
|
|
111 |
with gr.Row():
|
112 |
family_btn = gr.Button("π¨βπ©βπ§βπ¦ Family", elem_classes="law-button")
|
113 |
corporate_btn = gr.Button("π’ Corporate", elem_classes="law-button")
|
@@ -138,17 +139,22 @@ with gr.Blocks(theme = theme, css = custom_css) as demo:
|
|
138 |
|
139 |
submit_btn = gr.Button("Send",variant="primary")
|
140 |
|
141 |
-
def submit(country, custom_country, language, scenario,
|
142 |
selected_country = custom_country if country == "Other" else country
|
143 |
-
|
144 |
-
|
|
|
|
|
|
|
145 |
|
146 |
country_input.change(lambda c: gr.update(visible=c == "Other"), inputs=country_input, outputs=custom_country_input)
|
147 |
|
|
|
|
|
148 |
submit_btn.click(
|
149 |
submit,
|
150 |
-
inputs=[country_input, custom_country_input, language_input, scenario_input,
|
151 |
-
outputs=[chatbot, scenario_input]
|
152 |
)
|
153 |
|
154 |
demo.launch()
|
|
|
4 |
|
5 |
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
6 |
|
7 |
+
def get_chatbot_response(user_message, country, language, conversation_history):
|
|
|
|
|
|
|
8 |
|
9 |
system_message = (
|
10 |
f"You are a lawyer specializing in providing concise and accurate legal information based on the laws in {country}. "
|
|
|
37 |
|
38 |
conversation_history.append({"role": "assistant", "content": response})
|
39 |
|
40 |
+
chat_display = [
|
41 |
+
(msg["content"], conversation_history[i + 1]["content"])
|
42 |
+
for i, msg in enumerate(conversation_history[:-1]) if msg["role"] == "user"
|
43 |
+
]
|
44 |
+
|
45 |
+
return conversation_history, chat_display
|
46 |
|
47 |
theme = gr.themes.Ocean(
|
48 |
text_size="lg",
|
|
|
85 |
"""
|
86 |
|
87 |
def clear_history():
|
|
|
|
|
88 |
return []
|
89 |
|
90 |
|
|
|
106 |
|
107 |
custom_country_input = gr.Textbox(label="Enter Country (if not listed)", visible=False)
|
108 |
|
109 |
+
chatbot = gr.Chatbot(label="π¬ Chat History", type="messages")
|
110 |
+
chatbot.clear(fn=clear_history, outputs=conversation_state)
|
111 |
+
|
112 |
with gr.Row():
|
113 |
family_btn = gr.Button("π¨βπ©βπ§βπ¦ Family", elem_classes="law-button")
|
114 |
corporate_btn = gr.Button("π’ Corporate", elem_classes="law-button")
|
|
|
139 |
|
140 |
submit_btn = gr.Button("Send",variant="primary")
|
141 |
|
142 |
+
def submit(country, custom_country, language, scenario, conversation_state):
|
143 |
selected_country = custom_country if country == "Other" else country
|
144 |
+
updated_history, chat_display = get_chatbot_response(
|
145 |
+
scenario, selected_country, language, conversation_state
|
146 |
+
)
|
147 |
+
return updated_history, chat_display, ""
|
148 |
+
|
149 |
|
150 |
country_input.change(lambda c: gr.update(visible=c == "Other"), inputs=country_input, outputs=custom_country_input)
|
151 |
|
152 |
+
conversation_state = gr.State([])
|
153 |
+
|
154 |
submit_btn.click(
|
155 |
submit,
|
156 |
+
inputs=[country_input, custom_country_input, language_input, scenario_input, conversation_state],
|
157 |
+
outputs=[conversation_state, chatbot, scenario_input]
|
158 |
)
|
159 |
|
160 |
demo.launch()
|