prithivMLmods commited on
Commit
9240e91
1 Parent(s): ceec427

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -72
app.py CHANGED
@@ -1,73 +1,101 @@
1
- #refer llama recipes for more info https://github.com/huggingface/huggingface-llama-recipes/blob/main/inference-api.ipynb
2
- #huggingface-llama-recipes : https://github.com/huggingface/huggingface-llama-recipes/tree/main
3
- import gradio as gr
4
- from openai import OpenAI
5
- import os
6
-
7
- css = '''
8
- .gradio-container{max-width: 1000px !important}
9
- h1{text-align:center}
10
- footer {
11
- visibility: hidden
12
- }
13
- '''
14
-
15
- ACCESS_TOKEN = os.getenv("HF_TOKEN")
16
-
17
- client = OpenAI(
18
- base_url="https://api-inference.huggingface.co/v1/",
19
- api_key=ACCESS_TOKEN,
20
- )
21
-
22
- def respond(
23
- message,
24
- history: list[tuple[str, str]],
25
- system_message,
26
- max_tokens,
27
- temperature,
28
- top_p,
29
- ):
30
- messages = [{"role": "system", "content": system_message}]
31
-
32
- for val in history:
33
- if val[0]:
34
- messages.append({"role": "user", "content": val[0]})
35
- if val[1]:
36
- messages.append({"role": "assistant", "content": val[1]})
37
-
38
- messages.append({"role": "user", "content": message})
39
-
40
- response = ""
41
-
42
- for message in client.chat.completions.create(
43
- model="meta-llama/Meta-Llama-3.1-8B-Instruct",
44
- max_tokens=max_tokens,
45
- stream=True,
46
- temperature=temperature,
47
- top_p=top_p,
48
- messages=messages,
49
- ):
50
- token = message.choices[0].delta.content
51
-
52
- response += token
53
- yield response
54
-
55
- demo = gr.ChatInterface(
56
- respond,
57
- additional_inputs=[
58
- gr.Textbox(value="", label="System message"),
59
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
60
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
61
- gr.Slider(
62
- minimum=0.1,
63
- maximum=1.0,
64
- value=0.95,
65
- step=0.05,
66
- label="Top-P",
67
- ),
68
-
69
- ],
70
- css=css
71
- )
72
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  demo.launch()
 
1
+ import gradio as gr
2
+ from openai import OpenAI
3
+ import os
4
+
5
+ # Custom CSS for the Deadpool theme
6
+ css = '''
7
+ .gradio-container {
8
+ max-width: 1000px !important;
9
+ background-color: black !important;
10
+ color: red !important;
11
+ font-family: 'Courier New', monospace !important;
12
+ padding: 20px !important;
13
+ border-radius: 5px !important;
14
+ }
15
+ h1 {
16
+ text-align: center;
17
+ color: red !important;
18
+ font-family: 'Impact', sans-serif !important;
19
+ }
20
+ footer {
21
+ visibility: hidden;
22
+ }
23
+ textarea, input, select, button {
24
+ background-color: black !important;
25
+ color: red !important;
26
+ border: 2px solid blue !important;
27
+ font-family: 'Courier New', monospace !important;
28
+ }
29
+ button:hover {
30
+ background-color: blue !important;
31
+ color: white !important;
32
+ }
33
+ .chatbot {
34
+ background-color: black !important;
35
+ color: red !important;
36
+ border: 2px solid blue !important;
37
+ font-family: 'Courier New', monospace !important;
38
+ }
39
+ '''
40
+
41
+ ACCESS_TOKEN = os.getenv("HF_TOKEN")
42
+
43
+ client = OpenAI(
44
+ base_url="https://api-inference.huggingface.co/v1/",
45
+ api_key=ACCESS_TOKEN,
46
+ )
47
+
48
+ def respond(
49
+ message,
50
+ history: list[tuple[str, str]],
51
+ system_message,
52
+ max_tokens,
53
+ temperature,
54
+ top_p,
55
+ ):
56
+ messages = [{"role": "system", "content": system_message}]
57
+
58
+ for val in history:
59
+ if val[0]:
60
+ messages.append({"role": "user", "content": val[0]})
61
+ if val[1]:
62
+ messages.append({"role": "assistant", "content": val[1]})
63
+
64
+ messages.append({"role": "user", "content": message})
65
+
66
+ response = ""
67
+
68
+ for message in client.chat.completions.create(
69
+ model="meta-llama/Meta-Llama-3.1-8B-Instruct",
70
+ max_tokens=max_tokens,
71
+ stream=True,
72
+ temperature=temperature,
73
+ top_p=top_p,
74
+ messages=messages,
75
+ ):
76
+ token = message.choices[0].delta.content
77
+
78
+ response += token
79
+ yield response
80
+
81
+ demo = gr.ChatInterface(
82
+ respond,
83
+ additional_inputs=[
84
+ gr.Textbox(value="", label="System message", lines=2),
85
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
86
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
87
+ gr.Slider(
88
+ minimum=0.1,
89
+ maximum=1.0,
90
+ value=0.95,
91
+ step=0.05,
92
+ label="Top-P",
93
+ ),
94
+ ],
95
+ css=css,
96
+ title="Deadpool Chat",
97
+ theme="default"
98
+ )
99
+
100
+ if __name__ == "__main__":
101
  demo.launch()