Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,70 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
-
|
| 4 |
-
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Initialize model and tokenizer
|
| 6 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 7 |
+
"microsoft/phi-3-mini-instruct",
|
| 8 |
+
trust_remote_code=True,
|
| 9 |
+
torch_dtype=torch.float32, # Force float32 for CPU compatibility
|
| 10 |
+
device_map="auto", # Automatically handles CPU-only environment
|
| 11 |
+
)
|
| 12 |
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-3-mini-instruct")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Create pipeline
|
| 16 |
+
pipe = pipeline(
|
| 17 |
+
"text-generation",
|
| 18 |
+
model=model,
|
| 19 |
+
tokenizer=tokenizer,
|
| 20 |
+
device=-1, # Force CPU usage (-1 = CPU)
|
| 21 |
+
)
|
| 22 |
|
| 23 |
+
def respond(message, chat_history):
|
| 24 |
+
# Format the chat history with system prompt
|
| 25 |
+
system_prompt = "You are a helpful AI assistant."
|
| 26 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 27 |
+
|
| 28 |
+
# Add previous chat history
|
| 29 |
+
for user_msg, bot_msg in chat_history:
|
| 30 |
+
messages.append({"role": "user", "content": user_msg})
|
| 31 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 32 |
+
|
| 33 |
+
# Add new user message
|
| 34 |
messages.append({"role": "user", "content": message})
|
| 35 |
+
|
| 36 |
+
# Format for the model
|
| 37 |
+
formatted_input = tokenizer.apply_chat_template(
|
|
|
|
| 38 |
messages,
|
| 39 |
+
tokenize=False,
|
| 40 |
+
add_generation_prompt=True
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Generate response
|
| 44 |
+
generation_args = {
|
| 45 |
+
"max_new_tokens": 200, # Reduced for CPU performance
|
| 46 |
+
"temperature": 0.0,
|
| 47 |
+
"do_sample": False,
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
output = pipe(formatted_input, **generation_args)
|
| 51 |
+
response = output[0]['generated_text']
|
| 52 |
+
|
| 53 |
+
# Remove the input text from the response
|
| 54 |
+
if response.startswith(formatted_input):
|
| 55 |
+
response = response[len(formatted_input):]
|
| 56 |
+
|
| 57 |
+
return response
|
| 58 |
|
| 59 |
+
# Create Gradio interface
|
|
|
|
|
|
|
|
|
|
| 60 |
demo = gr.ChatInterface(
|
| 61 |
respond,
|
| 62 |
+
chatbot=gr.Chatbot(height=400),
|
| 63 |
+
textbox=gr.Textbox(placeholder="Ask me anything...", container=False, scale=7),
|
| 64 |
+
title="Phi-3 Mini Chat (CPU)",
|
| 65 |
+
examples=["What's 2x + 3 = 7?", "How to make banana smoothie?", "Explain quantum computing simply"],
|
| 66 |
+
cache_examples=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
)
|
| 68 |
|
|
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
+
demo.launch()
|