FrederickSundeep commited on
Commit
daf4465
·
1 Parent(s): fefedde

update commit with phi-3 mini 2

Browse files
Files changed (1) hide show
  1. app.py +22 -11
app.py CHANGED
@@ -5,7 +5,9 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
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)
@@ -18,20 +20,29 @@ def chat_fn(message, history):
18
  prompt = f"{history_text}<|user|>\n{message}\n<|assistant|>\n"
19
  response = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)[0]['generated_text']
20
  reply = response.split("<|assistant|>")[-1].strip()
 
 
 
 
 
21
  return reply
22
 
23
- # Wrap with Blocks for custom layout and auth
24
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
25
  gr.Markdown("## 💬 Chat with Phi-3 Mini")
26
- gr.Markdown("Welcome! Lightweight, fast, and privacy-focused AI assistant powered by Microsoft's Phi-3. You can ask questions, request code, or chat naturally.")
 
 
 
27
 
28
- gr.ChatInterface(fn=chat_fn,
29
- examples=["What is a large language model?", "Write a Python function to reverse a list.", "Explain the concept of recursion."],
30
- title="",
31
- retry_btn=" Retry",
32
- undo_btn=" Undo",
33
- clear_btn="🧹 Clear"
34
- )
 
 
35
 
36
- # Add authentication here
37
  demo.launch(auth=("user", "pass"))
 
5
  model_id = "microsoft/phi-3-mini-4k-instruct"
6
  tokenizer = AutoTokenizer.from_pretrained(model_id)
7
  model = AutoModelForCausalLM.from_pretrained(
8
+ model_id,
9
+ torch_dtype="auto",
10
+ device_map="auto" # Will default to CPU if no GPU available
11
  )
12
 
13
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
 
20
  prompt = f"{history_text}<|user|>\n{message}\n<|assistant|>\n"
21
  response = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)[0]['generated_text']
22
  reply = response.split("<|assistant|>")[-1].strip()
23
+
24
+ # Format as Markdown code block if reply looks like code
25
+ if "```" not in reply and any(keyword in reply for keyword in ["def ", "class ", "import ", ";", "{", "}"]):
26
+ reply = f"```\n{reply}\n```"
27
+
28
  return reply
29
 
30
+ # Launch the app with basic Blocks + authentication
31
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
32
  gr.Markdown("## 💬 Chat with Phi-3 Mini")
33
+ gr.Markdown(
34
+ "Welcome! A lightweight, privacy-friendly AI assistant powered by Microsoft's Phi-3 Mini.\n"
35
+ "Ask any question or request code examples. Use your credentials to access this demo."
36
+ )
37
 
38
+ gr.ChatInterface(
39
+ fn=chat_fn,
40
+ examples=[
41
+ "What is a large language model?",
42
+ "Write a Python function to reverse a list.",
43
+ "Explain the concept of recursion."
44
+ ],
45
+ title="",
46
+ )
47
 
 
48
  demo.launch(auth=("user", "pass"))