richardkimsm89 commited on
Commit
039036e
·
verified ·
1 Parent(s): 8b16688

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -12
app.py CHANGED
@@ -1,13 +1,12 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # Inference
5
- #model_text = "microsoft/Phi-4-mini-instruct"
6
- model_text = "microsoft/Phi-3.5-mini-instruct"
7
-
8
  client = InferenceClient()
9
 
10
- def fn_text(
 
 
 
11
  prompt,
12
  history,
13
  system_prompt,
@@ -16,17 +15,67 @@ def fn_text(
16
  top_p,
17
  ):
18
 
 
19
  messages = [{"role": "system", "content": system_prompt}]
20
  history.append(messages[0])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  messages.append({"role": "user", "content": prompt})
23
  history.append(messages[1])
24
 
 
25
  #messages = [{"role": "user", "content": prompt}]
26
  #history.append(messages[0])
27
 
28
  stream = client.chat.completions.create(
29
- model = model_text,
30
  messages = history,
31
  max_tokens = max_tokens,
32
  temperature = temperature,
@@ -39,8 +88,8 @@ def fn_text(
39
  chunks.append(chunk.choices[0].delta.content or "")
40
  yield "".join(chunks)
41
 
42
- app_text = gr.ChatInterface(
43
- fn = fn_text,
44
  type = "messages",
45
  additional_inputs = [
46
  gr.Textbox(value="You are a helpful assistant.", label="System Prompt"),
@@ -48,11 +97,11 @@ app_text = gr.ChatInterface(
48
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
49
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
50
  ],
51
- title = "Microsoft Phi",
52
- description = model_text,
53
  )
54
 
55
  app = gr.TabbedInterface(
56
- [app_text],
57
- ["Text"]
58
  ).launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
 
4
  client = InferenceClient()
5
 
6
+ # Phi 3
7
+ model_phi_3 = "microsoft/Phi-3.5-mini-instruct"
8
+
9
+ def fn_phi_3(
10
  prompt,
11
  history,
12
  system_prompt,
 
15
  top_p,
16
  ):
17
 
18
+ # With System Prompt
19
  messages = [{"role": "system", "content": system_prompt}]
20
  history.append(messages[0])
21
+ messages.append({"role": "user", "content": prompt})
22
+ history.append(messages[1])
23
+
24
+ # Without System Prompt
25
+ #messages = [{"role": "user", "content": prompt}]
26
+ #history.append(messages[0])
27
+
28
+ stream = client.chat.completions.create(
29
+ model = model_phi_3,
30
+ messages = history,
31
+ max_tokens = max_tokens,
32
+ temperature = temperature,
33
+ top_p = top_p,
34
+ stream = True,
35
+ )
36
+
37
+ chunks = []
38
+ for chunk in stream:
39
+ chunks.append(chunk.choices[0].delta.content or "")
40
+ yield "".join(chunks)
41
 
42
+ app_phi_3 = gr.ChatInterface(
43
+ fn = fn_phi_3,
44
+ type = "messages",
45
+ additional_inputs = [
46
+ gr.Textbox(value="You are a helpful assistant.", label="System Prompt"),
47
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens"),
48
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
49
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
50
+ ],
51
+ title = "Microsoft Phi 3",
52
+ description = model_phi_3,
53
+ )
54
+
55
+ # Phi 4
56
+ model_phi_4 = "microsoft/phi-4"
57
+
58
+ def fn_phi_4(
59
+ prompt,
60
+ history,
61
+ system_prompt,
62
+ max_tokens,
63
+ temperature,
64
+ top_p,
65
+ ):
66
+
67
+ # With System Prompt
68
+ messages = [{"role": "system", "content": system_prompt}]
69
+ history.append(messages[0])
70
  messages.append({"role": "user", "content": prompt})
71
  history.append(messages[1])
72
 
73
+ # Without System Prompt
74
  #messages = [{"role": "user", "content": prompt}]
75
  #history.append(messages[0])
76
 
77
  stream = client.chat.completions.create(
78
+ model = model_phi_4,
79
  messages = history,
80
  max_tokens = max_tokens,
81
  temperature = temperature,
 
88
  chunks.append(chunk.choices[0].delta.content or "")
89
  yield "".join(chunks)
90
 
91
+ app_phi_4 = gr.ChatInterface(
92
+ fn = fn_phi_4,
93
  type = "messages",
94
  additional_inputs = [
95
  gr.Textbox(value="You are a helpful assistant.", label="System Prompt"),
 
97
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
98
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
99
  ],
100
+ title = "Microsoft Phi 4",
101
+ description = model_phi_4,
102
  )
103
 
104
  app = gr.TabbedInterface(
105
+ [app_phi_3, app_phi_4],
106
+ ["Phi 3", "Phi 4"]
107
  ).launch()