richardkimsm89 commited on
Commit
399bbc5
·
verified ·
1 Parent(s): 0b95001

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -13
app.py CHANGED
@@ -4,12 +4,11 @@ from huggingface_hub import InferenceClient
4
  client = InferenceClient()
5
 
6
  # Gemma 2
7
- model_gemma_2 = "google/gemma-2-9b-it"
8
 
9
  def fn_gemma_2(
10
  prompt,
11
  history,
12
- #input,
13
  #system_prompt,
14
  max_tokens,
15
  temperature,
@@ -26,7 +25,70 @@ def fn_gemma_2(
26
  #messages.append({"role": "user", "content": prompt})
27
  #history.append(messages[1])
28
 
29
- #messages = [
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  # {
31
  # "role": "user",
32
  # "content": [
@@ -42,11 +104,11 @@ def fn_gemma_2(
42
  # }
43
  # ]
44
  # }
45
- #]
46
- #history.append(messages[0])
47
 
48
  stream = client.chat.completions.create(
49
- model = model_gemma_2,
50
  messages = history,
51
  max_tokens = max_tokens,
52
  temperature = temperature,
@@ -59,21 +121,21 @@ def fn_gemma_2(
59
  chunks.append(chunk.choices[0].delta.content or "")
60
  yield "".join(chunks)
61
 
62
- app_gemma_2 = gr.ChatInterface(
63
- fn = fn_gemma_2,
64
  type = "messages",
65
  additional_inputs = [
66
- #gr.Textbox(label="Input"),
67
  #gr.Textbox(value="You are a helpful assistant.", label="System Prompt"),
68
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens"),
69
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
70
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
71
  ],
72
- title = "Google Gemma 2",
73
- description = model_gemma_2,
74
  )
75
 
76
  app = gr.TabbedInterface(
77
- [app_gemma_2],
78
- ["Gemma 2"]
79
  ).launch()
 
4
  client = InferenceClient()
5
 
6
  # Gemma 2
7
+ model_gemma_2 = "google/gemma-2-27b-it"
8
 
9
  def fn_gemma_2(
10
  prompt,
11
  history,
 
12
  #system_prompt,
13
  max_tokens,
14
  temperature,
 
25
  #messages.append({"role": "user", "content": prompt})
26
  #history.append(messages[1])
27
 
28
+ stream = client.chat.completions.create(
29
+ model = model_gemma_2,
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_gemma_2 = gr.ChatInterface(
43
+ fn = fn_gemma_2,
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 = "Google Gemma 2",
52
+ description = model_gemma_2,
53
+ )
54
+
55
+ # Gemma 3
56
+ model_gemma_3 = "google/gemma-3-27b-it"
57
+
58
+ def fn_gemma_3(
59
+ prompt,
60
+ history,
61
+ input,
62
+ #system_prompt,
63
+ max_tokens,
64
+ temperature,
65
+ top_p,
66
+ ):
67
+
68
+ # Without System Prompt
69
+ messages = [
70
+ {
71
+ "role": "user",
72
+ "content": [
73
+ {
74
+ "type": "text",
75
+ "text": prompt
76
+ },
77
+ {
78
+ "type": "image_url",
79
+ "image_url": {
80
+ "url": input
81
+ }
82
+ }
83
+ ]
84
+ }
85
+ ]
86
+ history.append(messages[0])
87
+
88
+ # With System Prompt
89
+ #messages = [{"role": "system", "content": system_prompt}]
90
+ #history.append(messages[0])
91
+ #messages.append([
92
  # {
93
  # "role": "user",
94
  # "content": [
 
104
  # }
105
  # ]
106
  # }
107
+ #])
108
+ #history.append(messages[1])
109
 
110
  stream = client.chat.completions.create(
111
+ model = model_gemma_3,
112
  messages = history,
113
  max_tokens = max_tokens,
114
  temperature = temperature,
 
121
  chunks.append(chunk.choices[0].delta.content or "")
122
  yield "".join(chunks)
123
 
124
+ app_gemma_3 = gr.ChatInterface(
125
+ fn = fn_gemma_3,
126
  type = "messages",
127
  additional_inputs = [
128
+ gr.Textbox(label="Input"),
129
  #gr.Textbox(value="You are a helpful assistant.", label="System Prompt"),
130
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens"),
131
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
132
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
133
  ],
134
+ title = "Google Gemma 3",
135
+ description = model_gemma_3,
136
  )
137
 
138
  app = gr.TabbedInterface(
139
+ [app_gemma_2, app_gemma_3],
140
+ ["Gemma 2", "Gemma 3"]
141
  ).launch()