cheneli commited on
Commit
2953265
verified
1 Parent(s): d4b1a4b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -55
app.py CHANGED
@@ -1,63 +1,118 @@
 
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,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
60
-
61
-
62
- if __name__ == "__main__":
63
- demo.launch()
 
1
+ import os
2
  import gradio as gr
3
+ from http import HTTPStatus
4
+ import openai
5
+ from typing import Generator, List, Optional, Tuple, Dict
6
+ from urllib.error import HTTPError
7
 
8
+ API_URL = os.getenv('API_URL')
9
+ API_KEY = os.getenv('API_KEY')
10
+ CUSTOM_JS = os.getenv('CUSTOM_JS', None)
11
+ oai_client = openai.OpenAI(api_key=API_KEY, base_url=API_URL)
12
 
13
+ History = List[Tuple[str, str]]
14
+ Messages = List[Dict[str, str]]
15
 
16
+ def clear_session() -> History:
17
+ return '', []
 
 
 
 
 
 
 
18
 
19
+ def history_to_messages(history: History) -> Messages:
20
+ messages = []
21
+ for h in history:
22
+ messages.append({'role': 'user', 'content': h[0].strip()})
23
+ messages.append({'role': 'assistant', 'content': h[1].strip()})
24
+ return messages
25
 
26
+ def messages_to_history(messages: Messages) -> Tuple[str, History]:
27
+ history = []
28
+ for q, r in zip(messages[0::2], messages[1::2]):
29
+ history.append([q['content'], r['content']])
30
+ return history
31
 
32
+ def model_chat(query: Optional[str], history: Optional[History]) -> Generator[Tuple[str, History], None, None]:
33
+ if query is None:
34
+ query = ''
35
+ if history is None:
36
+ history = []
37
+ if not query.strip():
38
+ return
39
+ messages = history_to_messages(history)
40
+ messages.append({'role': 'user', 'content': query.strip()})
41
+ gen = oai_client.chat.completions.create(
42
+ model='dicta-il/dictalm2.0-instruct',
43
+ messages=messages,
44
+ temperature=0.7,
45
+ max_tokens=1024,
46
+ top_p=0.9,
47
+ stream=True
48
+ )
49
+ full_response = ''
50
+ for completion in gen:
51
+ text = completion.choices[0].delta.content
52
+ full_response += text or ''
53
+ yield full_response
54
+
55
+ with gr.Blocks(css='''
56
+ .gr-group {direction: rtl;}
57
+ .chatbot{text-align:right;}
58
+ .dicta-header {
59
+ background-color: var(--input-background-fill); /* Replace with desired background color */
60
+ border-radius: 10px;
61
+ padding: 20px;
62
+ text-align: center;
63
+ display: flex;
64
+ flex-direction: row;
65
+ align-items: center;
66
+ box-shadow: var(--block-shadow);
67
+ border-color: var(--block-border-color);
68
+ border-width: 1px;
69
+ }
70
+
71
+
72
+ @media (max-width: 768px) {
73
+ .dicta-header {
74
+ flex-direction: column; /* Change to vertical for mobile devices */
75
+ }
76
+ }
77
+ .chatbot.prose {
78
+ font-size: 1.2em;
79
+ }
80
+ .dicta-logo {
81
+ width: 150px; /* Replace with actual logo width as desired */
82
+ height: auto;
83
+ margin-bottom: 20px;
84
+ }
85
+ .dicta-intro-text {
86
+ margin-bottom: 20px;
87
+ text-align: center;
88
+ display: flex;
89
+ flex-direction: column;
90
+ align-items: center;
91
+ width: 100%;
92
+ font-size: 1.1em;
93
+ }
94
+
95
+ textarea {
96
+ font-size: 1.2em;
97
+ }
98
+ ''', js=CUSTOM_JS) as demo:
99
+ gr.Markdown("""
100
+ <div class="dicta-header">
101
+
102
+ <div class="dicta-intro-text">
103
+ <h1>DictaLM 2.0 - Instruct Chat Demo</h1>
104
+ <p>Welcome to the interactive demo of DictaLM-2.0. Explore the capabilities of our model and see how it can assist with your tasks.<br/>
105
+ <span dir='rtl'> 讘专讜讻讬诐 讛讘讗讬诐 诇讚诪讜 讛讗讬谞讟专讗拽讟讬讘讬 砖诇 DictaLM-2.0. 讞拽专讜 讗转 讬讻讜诇讜转 讛诪讜讚诇 砖诇谞讜 讜专讗讜 讻讬爪讚 讛讜讗 讬讻讜诇 诇住讬讬注 诇讻诐 讘诪砖讬诪讜转讬讻诐.</span><br/>
106
+ <span dir='rtl'> 讛诪讜讚诇 诪砖讜讞专专 诇谞讞诇转 讛讻诇诇 讜讗驻砖专 诇讛讜专讬讚讜 讘拽讬砖讜专: <a href="https://huggingface.co/dicta-il/dictalm2.0-instruct">讻讗谉</a></span></p>
107
+ </div>
108
+ </div>
109
+ """)
110
+
111
+ interface = gr.ChatInterface(model_chat, fill_height=False)
112
+ interface.chatbot.rtl = True
113
+ interface.textbox.placeholder = "讛讻谞住 砖讗诇讛 讘注讘专讬转 (讗讜 讘讗谞讙诇讬转!)"
114
+ interface.textbox.rtl = True
115
+ interface.textbox.text_align = 'right'
116
+ interface.theme_css += '.gr-group {direction: rtl !important;}'
117
 
118
+ demo.queue(api_open=False).launch(max_threads=20, share=False)