richardkimsm89 commited on
Commit
7d5b9b1
·
verified ·
1 Parent(s): 2a4b25a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -25
app.py CHANGED
@@ -1,15 +1,12 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
4
- # Inference
5
-
6
- #model_text = "meta-llama/Llama-4-Scout-17B-16E-Instruct"
7
- model_text = "meta-llama/Llama-3.2-3B-Instruct"
8
- model_vision = "meta-llama/Llama-3.2-11B-Vision-Instruct"
9
-
10
  client = InferenceClient()
11
 
12
- def fn_text(
 
 
 
13
  prompt,
14
  history,
15
  system_prompt,
@@ -18,14 +15,14 @@ def fn_text(
18
  top_p,
19
  ):
20
 
 
21
  messages = [{"role": "system", "content": [{"type": "text", "text": system_prompt}]}]
22
  history.append(messages[0])
23
-
24
  messages.append({"role": "user", "content": [{"type": "text", "text": prompt}]})
25
  history.append(messages[1])
26
 
27
  stream = client.chat.completions.create(
28
- model = model_text,
29
  messages = history,
30
  max_tokens = max_tokens,
31
  temperature = temperature,
@@ -39,7 +36,7 @@ def fn_text(
39
  yield "".join(chunks)
40
 
41
  app_text = gr.ChatInterface(
42
- fn = fn_text,
43
  type = "messages",
44
  additional_inputs = [
45
  gr.Textbox(value="You are a helpful assistant.", label="System Prompt"),
@@ -47,11 +44,14 @@ app_text = gr.ChatInterface(
47
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
48
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
49
  ],
50
- title = "Meta Llama",
51
- description = model_text,
52
  )
53
 
54
- def fn_vision(
 
 
 
55
  prompt,
56
  image_url,
57
  #system_prompt,
@@ -59,13 +59,14 @@ def fn_vision(
59
  temperature,
60
  top_p,
61
  ):
62
- messages = [{"role": "user", "content": [{"type": "text", "text": prompt}]}]
63
 
 
 
64
  if image_url:
65
  messages[0]["content"].append({"type": "image_url", "image_url": {"url": image_url}})
66
 
67
  stream = client.chat.completions.create(
68
- model = model_vision,
69
  messages = messages,
70
  max_tokens = max_tokens,
71
  temperature = temperature,
@@ -78,8 +79,8 @@ def fn_vision(
78
  chunks.append(chunk.choices[0].delta.content or "")
79
  yield "".join(chunks)
80
 
81
- app_vision = gr.Interface(
82
- fn = fn_vision,
83
  inputs = [
84
  gr.Textbox(label="Prompt"),
85
  gr.Textbox(label="Image URL")
@@ -93,14 +94,11 @@ app_vision = gr.Interface(
93
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
94
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
95
  ],
96
- title = "Meta Llama",
97
- description = model_vision,
98
  )
99
 
100
  app = gr.TabbedInterface(
101
- [app_text, app_vision],
102
- ["Text", "Vision"]
103
- ).launch()
104
-
105
- #if __name__ == "__main__":
106
- # app.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
 
 
 
 
 
 
 
4
  client = InferenceClient()
5
 
6
+ # Llama 3 - Text
7
+ model_llama_3_text = "meta-llama/Llama-3.2-3B-Instruct"
8
+
9
+ def fn_llama_3_text(
10
  prompt,
11
  history,
12
  system_prompt,
 
15
  top_p,
16
  ):
17
 
18
+ # With System Prompt
19
  messages = [{"role": "system", "content": [{"type": "text", "text": system_prompt}]}]
20
  history.append(messages[0])
 
21
  messages.append({"role": "user", "content": [{"type": "text", "text": prompt}]})
22
  history.append(messages[1])
23
 
24
  stream = client.chat.completions.create(
25
+ model = model_llama_3_text,
26
  messages = history,
27
  max_tokens = max_tokens,
28
  temperature = temperature,
 
36
  yield "".join(chunks)
37
 
38
  app_text = gr.ChatInterface(
39
+ fn = fn_llama_3_text,
40
  type = "messages",
41
  additional_inputs = [
42
  gr.Textbox(value="You are a helpful assistant.", label="System Prompt"),
 
44
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
45
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
46
  ],
47
+ title = "Meta Llama 3",
48
+ description = model_llama_3_text,
49
  )
50
 
51
+ # Llama 3 - Vision
52
+ model_llama_3_vision = "meta-llama/Llama-3.2-11B-Vision-Instruct"
53
+
54
+ def fn_llama_3_vision(
55
  prompt,
56
  image_url,
57
  #system_prompt,
 
59
  temperature,
60
  top_p,
61
  ):
 
62
 
63
+ # Without System Prompt
64
+ messages = [{"role": "user", "content": [{"type": "text", "text": prompt}]}]
65
  if image_url:
66
  messages[0]["content"].append({"type": "image_url", "image_url": {"url": image_url}})
67
 
68
  stream = client.chat.completions.create(
69
+ model = model_llama_3_vision,
70
  messages = messages,
71
  max_tokens = max_tokens,
72
  temperature = temperature,
 
79
  chunks.append(chunk.choices[0].delta.content or "")
80
  yield "".join(chunks)
81
 
82
+ app_llama_3_vision = gr.Interface(
83
+ fn = fn_llama_3_vision,
84
  inputs = [
85
  gr.Textbox(label="Prompt"),
86
  gr.Textbox(label="Image URL")
 
94
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
95
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-P"),
96
  ],
97
+ title = "Meta Llama 3",
98
+ description = model_llama_3_vision,
99
  )
100
 
101
  app = gr.TabbedInterface(
102
+ [app_llama_3_text, app_llama_3_vision],
103
+ ["Llama 3 - Text", "Llama 3 - Vision"]
104
+ ).launch()