FrederickSundeep commited on
Commit
e70bcc1
·
1 Parent(s): f74f344

update commit with phi-3 mini

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -4,12 +4,13 @@ from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
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(model_id, torch_dtype="auto", device_map="auto")
 
 
8
 
9
- # Pipeline for text generation
10
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
11
 
12
- # Chat function with history
13
  def chat_fn(message, history):
14
  history_text = ""
15
  for user, bot in history:
@@ -19,19 +20,18 @@ def chat_fn(message, history):
19
  reply = response.split("<|assistant|>")[-1].strip()
20
  return reply
21
 
22
- # Create Chat Interface with authentication and style
23
- chat = gr.ChatInterface(
24
- fn=chat_fn,
25
- title="💬 Chat with Phi-3 Mini",
26
- description="A lightweight ChatGPT-like assistant powered by Microsoft's Phi-3 Mini. Feel free to ask questions or code!",
27
- theme="soft", # Light elegant theme
28
- examples=["What is Python?", "Write a Python function to reverse a list.", "How does an LLM work?"],
29
- auth=[("user", "pass")], # Replace with secure creds in real use
30
- additional_inputs=[],
31
- submit_btn="Send",
32
- retry_btn=" Retry",
33
- undo_btn="⤺ Undo",
34
- clear_btn="🧹 Clear Chat"
35
- )
36
 
37
- chat.launch()
 
 
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
+ # Chat logic with history
14
  def chat_fn(message, history):
15
  history_text = ""
16
  for user, bot in history:
 
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"))