Anjo123 commited on
Commit
17ba600
Β·
verified Β·
1 Parent(s): a49d213

Upload chatbot.py

Browse files
Files changed (1) hide show
  1. chatbot.py +145 -0
chatbot.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from groq import Groq
2
+ import gradio as gr
3
+
4
+ client = Groq(api_key=GROQ_API_KEY)
5
+
6
+ conversation_history = []
7
+
8
+ def get_chatbot_response(user_message, country, language):
9
+ global conversation_history
10
+
11
+ conversation_history.append({"role": "user", "content": user_message})
12
+
13
+ if len(conversation_history) == 1:
14
+ conversation_history.insert(0, {
15
+ "role": "system",
16
+ "content": f"You are a lawyer specializing in providing concise and accurate legal information based on the laws in {country}. Respond in {language}. Provide clear, factual information without offering personal legal advice or opinions. Include relevant legal references, statutes, or case law when possible."
17
+ })
18
+
19
+ completion = client.chat.completions.create(
20
+ model="deepseek-r1-distill-llama-70b",
21
+ messages=conversation_history,
22
+ temperature=0.3,
23
+ top_p=0.95,
24
+ stream=True,
25
+ reasoning_format="hidden"
26
+ )
27
+
28
+ response = ""
29
+ for chunk in completion:
30
+ response += chunk.choices[0].delta.content or ""
31
+
32
+ conversation_history.append({"role": "assistant", "content": response})
33
+
34
+ return [(msg["content"], conversation_history[i + 1]["content"]) for i, msg in enumerate(conversation_history[:-1]) if msg["role"] == "user"]
35
+
36
+ theme = gr.themes.Ocean(
37
+ text_size="lg",
38
+ font=[gr.themes.GoogleFont('DM Sans'), 'ui-sans-serif', 'system-ui', 'sans-serif'],
39
+ ).set(
40
+ body_text_size='*text_lg',
41
+ background_fill_secondary='*secondary_100',
42
+ chatbot_text_size='*text_lg',
43
+ input_radius='*radius_md',
44
+ input_text_size='*text_lg',
45
+ )
46
+
47
+ custom_css = """
48
+ .title-text {
49
+ background: #00A0B0;
50
+ -webkit-background-clip: text;
51
+ background-clip: text;
52
+ color: transparent;
53
+ -webkit-text-fill-color: transparent;
54
+ display: inline-block;
55
+ width: fit-content;
56
+ font-weight: bold;
57
+ text-align: center;
58
+ font-size: 45px;
59
+ }
60
+
61
+ .law-button {
62
+ border: 1px solid #00A0B0;
63
+ background-color: transparent;
64
+ font-size: 15px;
65
+ padding: 5px 15px;
66
+ border-radius: 16px;
67
+ margin: 0 5px;
68
+ }
69
+
70
+ .law-button:hover {
71
+ background: linear-gradient(90deg, #00A0B0, #00FFEF);
72
+ color: white;
73
+ }
74
+ """
75
+
76
+ def clear_history():
77
+ global conversation_history
78
+ conversation_history = []
79
+ return []
80
+
81
+
82
+ with gr.Blocks(theme = theme, css = custom_css) as demo:
83
+ gr.HTML("<h2 class='title-text'>βš–οΈ AI Legal Chatbot</h2>")
84
+ gr.Markdown("### Hey there! Pick your country, choose a language, and tell us about your legal situation. We're here to help!")
85
+
86
+ with gr.Row():
87
+ country_input = gr.Dropdown(
88
+ ["Canada", "United States", "United Kingdom", "Spain", "France", "Germany", "India", "China", "Lebanon", "Other"],
89
+ label="🌍 Select Country",
90
+ interactive=True
91
+ )
92
+ language_input = gr.Dropdown(
93
+ ["English", "Spanish", "French", "German", "Hindi", "Mandarin", "Arabic", "Other"],
94
+ label="πŸ—£οΈ Select Language",
95
+ interactive=True
96
+ )
97
+
98
+ custom_country_input = gr.Textbox(label="Enter Country (if not listed)", visible=False)
99
+
100
+ chatbot = gr.Chatbot(label="πŸ’¬ Chat History")
101
+ chatbot.clear(fn=clear_history, outputs=chatbot)
102
+ with gr.Row():
103
+ family_btn = gr.Button("πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦ Family", elem_classes="law-button")
104
+ corporate_btn = gr.Button("🏒 Corporate", elem_classes="law-button")
105
+ health_btn = gr.Button("πŸ₯ Health", elem_classes="law-button")
106
+ military_btn = gr.Button("πŸŽ–οΈ Military", elem_classes="law-button")
107
+ immigration_btn = gr.Button("🌍 Immigration", elem_classes="law-button")
108
+ criminal_btn = gr.Button("πŸš” Criminal", elem_classes="law-button")
109
+ property_btn = gr.Button("🏠 Property", elem_classes="law-button")
110
+ environmental_btn = gr.Button("🌱 Environmental", elem_classes="law-button")
111
+
112
+
113
+ scenario_input = gr.Textbox(label="πŸ’‘ Type your message...", placeholder="Describe your legal situation...", interactive=True)
114
+
115
+ def update_law_selection(current, new_selection):
116
+ if "Law:" in current:
117
+ parts = current.split("Law:", 1)
118
+ additional_text = parts[1] if len(parts) > 1 else ""
119
+ else:
120
+ additional_text = current
121
+
122
+ return f"{new_selection} Law: {additional_text}"
123
+
124
+ for btn, law in zip(
125
+ [family_btn, corporate_btn, health_btn, military_btn, immigration_btn, criminal_btn, property_btn, environmental_btn],
126
+ ["Family", "Corporate", "Health", "Military", "Immigration", "Criminal", "Property", "Environmental"]
127
+ ):
128
+ btn.click(lambda current, law=law: update_law_selection(current, law), inputs=scenario_input, outputs=scenario_input)
129
+
130
+ submit_btn = gr.Button("Send",variant="primary")
131
+
132
+ def submit(country, custom_country, language, scenario, chat_history):
133
+ selected_country = custom_country if country == "Other" else country
134
+ new_chat_history = get_chatbot_response(scenario, selected_country, language)
135
+ return new_chat_history, ""
136
+
137
+ country_input.change(lambda c: gr.update(visible=c == "Other"), inputs=country_input, outputs=custom_country_input)
138
+
139
+ submit_btn.click(
140
+ submit,
141
+ inputs=[country_input, custom_country_input, language_input, scenario_input, chatbot],
142
+ outputs=[chatbot, scenario_input]
143
+ )
144
+
145
+ demo.launch()