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

update commit with phi-3 mini 1114

Browse files
Files changed (1) hide show
  1. app.py +12 -21
app.py CHANGED
@@ -1,26 +1,22 @@
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,18 +29,14 @@ def chat_fn(message, 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(
50
  fn=chat_fn,
@@ -57,5 +49,4 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
57
  chatbot=gr.Chatbot(type="messages")
58
  )
59
 
60
- # 🚀 Launch
61
- demo.launch(ssr_mode=False)
 
1
  import gradio as gr
 
2
  from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
3
 
4
+ # ✅ Safe import of the GPU decorator
5
  try:
6
+ from spaces import GPU
7
+ except ImportError:
8
+ def GPU(func): return func # Fallback if not in a HF Space
 
9
 
10
+ # Load Phi-3 Mini model
11
  model_id = "microsoft/phi-3-mini-4k-instruct"
12
  tokenizer = AutoTokenizer.from_pretrained(model_id)
13
  model = AutoModelForCausalLM.from_pretrained(
14
+ model_id, torch_dtype="auto", device_map="auto"
 
 
15
  )
 
16
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
17
 
18
+ # Apply GPU decorator to ensure ZeroGPU allocates GPU
19
+ @GPU
20
  def chat_fn(message, history):
21
  history_text = ""
22
  for item in history:
 
29
  result = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)[0]['generated_text']
30
  reply = result.split("<|assistant|>")[-1].strip()
31
 
32
+ if "```" not in reply and any(word in reply for word in ["def ", "class ", "import "]):
 
33
  reply = f"```\n{reply}\n```"
 
34
  return reply
35
 
36
+ # Gradio UI
37
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
38
  gr.Markdown("## 💬 Chat with Phi-3 Mini")
39
+ gr.Markdown("Welcome to your AI Assistant powered by Phi-3 Mini. Ask me anything or request code examples!")
 
 
40
 
41
  gr.ChatInterface(
42
  fn=chat_fn,
 
49
  chatbot=gr.Chatbot(type="messages")
50
  )
51
 
52
+ demo.launch()