FrederickSundeep commited on
Commit
2b29a6a
·
1 Parent(s): 6698e40

update commit with phi-3 mini 5

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -1,14 +1,19 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
 
 
 
 
 
3
 
4
  # Load Phi-3 Mini model
5
  model_id = "microsoft/phi-3-mini-4k-instruct"
6
  tokenizer = AutoTokenizer.from_pretrained(model_id)
7
  model = AutoModelForCausalLM.from_pretrained(
8
- model_id, torch_dtype="auto", device_map="auto"
9
- )
10
 
11
- pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
12
 
13
  # OpenAI-style messages (new format)
14
  def chat_fn(message, history):
@@ -29,11 +34,10 @@ def chat_fn(message, history):
29
 
30
  return reply
31
 
 
32
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
33
  gr.Markdown("## 💬 Chat with Phi-3 Mini")
34
- gr.Markdown(
35
- "Welcome to your AI Assistant powered by Phi-3 Mini. Ask me anything or request code examples!"
36
- )
37
 
38
  gr.ChatInterface(
39
  fn=chat_fn,
@@ -43,7 +47,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
43
  "Write a JavaScript function to reverse a string.",
44
  "Explain how transformers work.",
45
  ],
46
- chatbot=gr.Chatbot(type="messages") # fixes the deprecated tuples warning
47
  )
48
 
49
- demo.launch(share=True)
 
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
+ import torch
4
+
5
+ # Force device based on availability
6
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
+ print(f"Using device: {device}")
8
 
9
  # Load Phi-3 Mini model
10
  model_id = "microsoft/phi-3-mini-4k-instruct"
11
  tokenizer = AutoTokenizer.from_pretrained(model_id)
12
  model = AutoModelForCausalLM.from_pretrained(
13
+ model_id, torch_dtype=torch.float16 if device.type == "cuda" else torch.float32
14
+ ).to(device)
15
 
16
+ pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0 if device.type == "cuda" else -1)
17
 
18
  # OpenAI-style messages (new format)
19
  def chat_fn(message, history):
 
34
 
35
  return reply
36
 
37
+ # Gradio UI
38
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
39
  gr.Markdown("## 💬 Chat with Phi-3 Mini")
40
+ gr.Markdown("Lightweight AI Assistant powered by Microsoft's Phi-3 Mini. Works best with short prompts. Ask away!")
 
 
41
 
42
  gr.ChatInterface(
43
  fn=chat_fn,
 
47
  "Write a JavaScript function to reverse a string.",
48
  "Explain how transformers work.",
49
  ],
50
+ chatbot=gr.Chatbot(type="messages")
51
  )
52
 
53
+ # Launch without SSR and share (for Spaces)
54
+ demo.launch(debug=True, ssr_mode=False)