Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
|
|
3 |
|
4 |
-
|
5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
-
"""
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
9 |
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
response = ""
|
29 |
-
|
30 |
for message in client.chat_completion(
|
31 |
messages,
|
32 |
max_tokens=max_tokens,
|
@@ -35,29 +27,96 @@ def respond(
|
|
35 |
top_p=top_p,
|
36 |
):
|
37 |
token = message.choices[0].delta.content
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
"""
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
gr.Textbox(
|
49 |
-
gr.
|
50 |
-
|
51 |
-
gr.
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
],
|
59 |
-
)
|
60 |
-
|
61 |
-
|
62 |
-
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
os.system('pip install transformers')
|
3 |
+
os.system('pip install gradio')
|
4 |
+
os.system('pip install requests')
|
5 |
+
|
6 |
+
import requests
|
7 |
import gradio as gr
|
8 |
from huggingface_hub import InferenceClient
|
9 |
+
from transformers import pipeline
|
10 |
|
11 |
+
# Inference client for chat completion
|
|
|
|
|
12 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
13 |
|
14 |
+
# Different pipelines for different tasks
|
15 |
+
qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
16 |
|
17 |
+
def respond(message, system_message, max_tokens, temperature, top_p):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
messages = [{"role": "system", "content": system_message}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
messages.append({"role": "user", "content": message})
|
20 |
|
21 |
response = ""
|
|
|
22 |
for message in client.chat_completion(
|
23 |
messages,
|
24 |
max_tokens=max_tokens,
|
|
|
27 |
top_p=top_p,
|
28 |
):
|
29 |
token = message.choices[0].delta.content
|
30 |
+
if token is not None:
|
31 |
+
response += token
|
32 |
+
return response
|
33 |
|
34 |
+
def generate_defense_argument(case_details):
|
35 |
+
system_message = (
|
36 |
+
"You are an expert Defense Attorney. Provide the best and most detailed arguments "
|
37 |
+
"to defend the case based on the given case details. Include thorough analysis, "
|
38 |
+
"evidence presentation, and any relevant legal precedents."
|
39 |
+
)
|
40 |
+
arguments = respond(case_details, system_message, max_tokens=1024, temperature=0.7, top_p=0.95)
|
41 |
+
return arguments
|
42 |
|
43 |
+
# Custom CSS for a clean layout
|
44 |
+
custom_css = """
|
45 |
+
body {
|
46 |
+
background-color: #ffffff;
|
47 |
+
color: #000000;
|
48 |
+
font-family: Arial, sans-serif;
|
49 |
+
}
|
50 |
+
.gradio-container {
|
51 |
+
max-width: 1000px;
|
52 |
+
margin: 0 auto;
|
53 |
+
padding: 20px;
|
54 |
+
background-color: #ffffff;
|
55 |
+
border: 1px solid #e0e0e0;
|
56 |
+
border-radius: 8px;
|
57 |
+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
|
58 |
+
}
|
59 |
+
.gr-button {
|
60 |
+
background-color: #ffffff !important;
|
61 |
+
border-color: #ffffff !important;
|
62 |
+
color: #000000 !important;
|
63 |
+
margin: 5px;
|
64 |
+
}
|
65 |
+
.gr-button:hover {
|
66 |
+
background-color: #ffffff !important;
|
67 |
+
border-color: #004085 !important;
|
68 |
+
}
|
69 |
+
.gr-input, .gr-textbox, .gr-slider, .gr-markdown, .gr-chatbox {
|
70 |
+
border-radius: 4px;
|
71 |
+
border: 1px solid #ced4da;
|
72 |
+
background-color: #ffffff !important;
|
73 |
+
color: #000000 !important;
|
74 |
+
}
|
75 |
+
.gr-input:focus, .gr-textbox:focus, .gr-slider:focus {
|
76 |
+
border-color: #ffffff;
|
77 |
+
outline: 0;
|
78 |
+
box-shadow: 0 0 0 0.2rem rgba(255, 255, 255, 1.0);
|
79 |
+
}
|
80 |
+
#flagging-button {
|
81 |
+
display: none;
|
82 |
+
}
|
83 |
+
footer {
|
84 |
+
display: none;
|
85 |
+
}
|
86 |
+
.chatbox .chat-container .chat-message {
|
87 |
+
background-color: #ffffff !important;
|
88 |
+
color: #000000 !important;
|
89 |
+
}
|
90 |
+
.chatbox .chat-container .chat-message-input {
|
91 |
+
background-color: #ffffff !important;
|
92 |
+
color: #000000 !important;
|
93 |
+
}
|
94 |
+
.gr-markdown {
|
95 |
+
background-color: #ffffff !important;
|
96 |
+
color: #000000 !important;
|
97 |
+
}
|
98 |
+
.gr-markdown h1, .gr-markdown h2, .gr-markdown h3, .gr-markdown h4, .gr-markdown h5, .gr-markdown h6, .gr-markdown p, .gr-markdown ul, .gr-markdown ol, .gr-markdown li {
|
99 |
+
color: #000000 !important;
|
100 |
+
}
|
101 |
+
.score-box {
|
102 |
+
width: 60px;
|
103 |
+
height: 60px;
|
104 |
+
display: flex;
|
105 |
+
align-items: center
|
106 |
+
}
|
107 |
"""
|
108 |
+
|
109 |
+
# Gradio Interface
|
110 |
+
with gr.Blocks(css=custom_css) as demo:
|
111 |
+
with gr.Column():
|
112 |
+
gr.Markdown("# Defense Expert\n### Provide Case Details")
|
113 |
+
case_details = gr.Textbox(lines=5, placeholder="Enter case details here...")
|
114 |
+
defense_argument = gr.Textbox(lines=10, placeholder="Defense's Argument...")
|
115 |
+
|
116 |
+
generate_btn = gr.Button("Generate Argument")
|
117 |
+
generate_btn.click(generate_defense_argument, inputs=[case_details], outputs=[defense_argument])
|
118 |
+
|
119 |
+
clear_btn = gr.Button("Clear")
|
120 |
+
clear_btn.click(lambda: "", None, defense_argument)
|
121 |
+
|
122 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|