Spaces:
Sleeping
Sleeping
Commit ·
f74f344
1
Parent(s): c080904
initial commit with phi-3 mini
Browse files- app.py +37 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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(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:
|
| 16 |
+
history_text += f"<|user|>\n{user}\n<|assistant|>\n{bot}\n"
|
| 17 |
+
prompt = f"{history_text}<|user|>\n{message}\n<|assistant|>\n"
|
| 18 |
+
response = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)[0]['generated_text']
|
| 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()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
gradio
|
| 3 |
+
torch
|
| 4 |
+
accelerate
|