FrederickSundeep commited on
Commit
1549b17
·
1 Parent(s): 6addef4

update commit with phi-3 mini 1113

Browse files
Files changed (1) hide show
  1. app.py +20 -8
app.py CHANGED
@@ -1,16 +1,26 @@
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):
15
  history_text = ""
16
  for item in history:
@@ -23,16 +33,17 @@ def chat_fn(message, history):
23
  result = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)[0]['generated_text']
24
  reply = result.split("<|assistant|>")[-1].strip()
25
 
26
- # Format code blocks
27
- if "```" not in reply and any(word in reply for word in ["def ", "class ", "import "]):
28
  reply = f"```\n{reply}\n```"
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(
@@ -43,7 +54,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()
 
 
1
  import gradio as gr
2
+ import torch
3
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
4
 
5
+ # Force ZeroGPU to allocate GPU early
6
+ try:
7
+ _ = torch.tensor([1.0], device="cuda")
8
+ print("✅ ZeroGPU triggered successfully.")
9
+ except Exception as e:
10
+ print(f"⚠️ GPU allocation failed: {e}")
11
+
12
+ # 🚀 Load Phi-3 Mini model
13
  model_id = "microsoft/phi-3-mini-4k-instruct"
14
  tokenizer = AutoTokenizer.from_pretrained(model_id)
15
  model = AutoModelForCausalLM.from_pretrained(
16
+ model_id,
17
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
18
+ device_map="auto"
19
  )
20
 
21
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
22
 
23
+ # 💬 Chat function
24
  def chat_fn(message, history):
25
  history_text = ""
26
  for item in history:
 
33
  result = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)[0]['generated_text']
34
  reply = result.split("<|assistant|>")[-1].strip()
35
 
36
+ # Format code blocks if applicable
37
+ if "```" not in reply and any(w in reply for w in ["def ", "class ", "import "]):
38
  reply = f"```\n{reply}\n```"
39
 
40
  return reply
41
 
42
+ # 🧩 Gradio UI
43
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
44
  gr.Markdown("## 💬 Chat with Phi-3 Mini")
45
  gr.Markdown(
46
+ "Welcome to your AI Assistant powered by Phi-3 Mini and ZeroGPU (uses GPU if available)."
47
  )
48
 
49
  gr.ChatInterface(
 
54
  "Write a JavaScript function to reverse a string.",
55
  "Explain how transformers work.",
56
  ],
57
+ chatbot=gr.Chatbot(type="messages")
58
  )
59
 
60
+ # 🚀 Launch
61
+ demo.launch(ssr_mode=False)