Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,39 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
"
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return response
|
| 40 |
-
|
| 41 |
-
interface = gr.Interface(
|
| 42 |
-
fn=chatbot,
|
| 43 |
-
inputs="text",
|
| 44 |
-
outputs="text",
|
| 45 |
-
title="Mistral 7B Chatbot",
|
| 46 |
-
description="Ask questions and get answers from Mistral 7B!"
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
# Step 6: Launch the App
|
| 50 |
interface.launch()
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
+
import torch
|
| 3 |
+
|
| 4 |
+
# Load Zephyr 7B (no authentication required)
|
| 5 |
+
zephyr_tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-alpha")
|
| 6 |
+
zephyr_model = AutoModelForCausalLM.from_pretrained(
|
| 7 |
+
"HuggingFaceH4/zephyr-7b-alpha",
|
| 8 |
+
torch_dtype=torch.float16, # Use half-precision for faster inference
|
| 9 |
+
device_map="auto" # Automatically loads the model on GPU if available
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
def generate_response(prompt):
|
| 13 |
+
# Tokenize the input prompt
|
| 14 |
+
inputs = zephyr_tokenizer(prompt, return_tensors="pt").to(zephyr_model.device)
|
| 15 |
+
|
| 16 |
+
# Generate the response
|
| 17 |
+
outputs = zephyr_model.generate(**inputs, max_length=200)
|
| 18 |
+
|
| 19 |
+
# Decode the response
|
| 20 |
+
response = zephyr_tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 21 |
+
return response
|
| 22 |
+
|
| 23 |
+
import gradio as gr
|
| 24 |
+
|
| 25 |
+
# Gradio interface
|
| 26 |
+
def chatbot(prompt):
|
| 27 |
+
response = generate_response(prompt)
|
| 28 |
+
return response
|
| 29 |
+
|
| 30 |
+
interface = gr.Interface(
|
| 31 |
+
fn=chatbot,
|
| 32 |
+
inputs="text",
|
| 33 |
+
outputs="text",
|
| 34 |
+
title="Zephyr 7B Chatbot",
|
| 35 |
+
description="Ask questions and get answers from Zephyr 7B!"
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Launch the app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
interface.launch()
|