StevenChen16 commited on
Commit
b38ffc6
1 Parent(s): 3e4e679

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -15
app.py CHANGED
@@ -16,7 +16,7 @@ DESCRIPTION = '''
16
  LICENSE = """
17
  <p/>
18
  ---
19
- Built with model "StevenChen16/Llama3-8B-Lawyer", based on "meta-llama/Meta-Llama-3-8B"
20
  """
21
 
22
  PLACEHOLDER = """
@@ -37,6 +37,26 @@ h1 {
37
  background: #1565c0;
38
  border-radius: 100vh;
39
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  """
41
 
42
  args = dict(
@@ -86,26 +106,37 @@ def query_model(user_input, history):
86
  for new_text in chat_model.stream_chat(messages, max_new_tokens=512, temperature=0.9):
87
  response += new_text
88
  yield response
89
-
 
 
 
 
 
 
 
 
90
  # Gradio block
91
  chatbot = gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
92
 
93
  with gr.Blocks(css=css) as demo:
94
  gr.Markdown(DESCRIPTION)
95
  gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
96
- gr.ChatInterface(
97
- fn=query_model,
98
- chatbot=chatbot,
99
- examples=[
100
- ['What are the key differences between a sole proprietorship and a partnership?'],
101
- ['What legal steps should I take if I want to start a business in the US?'],
102
- ['Can you explain the concept of "duty of care" in negligence law?'],
103
- ['What are the legal requirements for obtaining a patent in Canada?'],
104
- ['How can I protect my intellectual property when sharing my idea with potential investors?']
105
- ],
106
- cache_examples=False,
107
- )
 
 
 
108
  gr.Markdown(LICENSE)
109
 
110
  if __name__ == "__main__":
111
- demo.launch()
 
16
  LICENSE = """
17
  <p/>
18
  ---
19
+ Built with model "StevenChen16/llama3-8B-Lawyer", based on "meta-llama/Meta-Llama-3-8B"
20
  """
21
 
22
  PLACEHOLDER = """
 
37
  background: #1565c0;
38
  border-radius: 100vh;
39
  }
40
+ .chat-message {
41
+ display: flex;
42
+ align-items: flex-start;
43
+ margin-bottom: 10px;
44
+ }
45
+ .chat-message img {
46
+ width: 40px;
47
+ height: 40px;
48
+ margin-right: 10px;
49
+ border-radius: 50%;
50
+ }
51
+ .chat-message .message {
52
+ max-width: 80%;
53
+ background-color: #f1f1f1;
54
+ padding: 10px;
55
+ border-radius: 10px;
56
+ }
57
+ .me .message {
58
+ background-color: #d1e7ff;
59
+ }
60
  """
61
 
62
  args = dict(
 
106
  for new_text in chat_model.stream_chat(messages, max_new_tokens=512, temperature=0.9):
107
  response += new_text
108
  yield response
109
+
110
+ # 格式化消息
111
+ def format_message(role, content):
112
+ if role == 'user':
113
+ avatar = '<div class="chat-message me"><img src="data:image/svg+xml,<svg xmlns=\'http://www.w3.org/2000/svg\' viewBox=\'0 0 100 100\'><rect width=\'100\' height=\'100\' fill=\'black\'/><text x=\'50%\' y=\'50%\' fill=\'white\' font-size=\'50\' text-anchor=\'middle\' alignment-baseline=\'central\'>Me</text></svg>" />'
114
+ else:
115
+ avatar = '<div class="chat-message"><img src="avatar.png" />'
116
+ return f'{avatar}<div class="message">{content}</div></div>'
117
+
118
  # Gradio block
119
  chatbot = gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
120
 
121
  with gr.Blocks(css=css) as demo:
122
  gr.Markdown(DESCRIPTION)
123
  gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
124
+
125
+ def respond(message, history):
126
+ formatted_user_message = format_message('user', message)
127
+ history.append((message, formatted_user_message))
128
+ response = query_model(message, history)
129
+ formatted_ai_response = format_message('ai', next(response))
130
+ history.append((message, formatted_ai_response))
131
+ return history, history
132
+
133
+ chatbot = gr.Chatbot(label='Gradio ChatInterface')
134
+ input_text = gr.Textbox(label="Input", placeholder="Type your question here...")
135
+ send_button = gr.Button("Send")
136
+
137
+ send_button.click(respond, [input_text, chatbot], [chatbot, chatbot])
138
+
139
  gr.Markdown(LICENSE)
140
 
141
  if __name__ == "__main__":
142
+ demo.launch(share=True)