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

update commit with phi-3 mini 3

Browse files
Files changed (1) hide show
  1. app.py +19 -18
app.py CHANGED
@@ -5,44 +5,45 @@ 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,
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)
14
 
15
- # Chat logic with history
16
  def chat_fn(message, history):
17
  history_text = ""
18
- for user, bot in history:
19
- history_text += f"<|user|>\n{user}\n<|assistant|>\n{bot}\n"
 
 
 
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"))
 
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:
17
+ if item["role"] == "user":
18
+ history_text += f"<|user|>\n{item['content']}\n"
19
+ elif item["role"] == "assistant":
20
+ history_text += f"<|assistant|>\n{item['content']}\n"
21
  prompt = f"{history_text}<|user|>\n{message}\n<|assistant|>\n"
 
 
22
 
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(
39
  fn=chat_fn,
40
+ title="",
41
  examples=[
42
+ "What is Python?",
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()