Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,53 +3,37 @@ from transformers import pipeline
|
|
| 3 |
import torch
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
|
| 12 |
# Load the model pipeline
|
| 13 |
generator = pipeline(
|
| 14 |
"text-generation",
|
| 15 |
-
model=
|
| 16 |
-
|
| 17 |
-
torch_dtype=
|
| 18 |
-
max_new_tokens=560
|
| 19 |
)
|
| 20 |
|
| 21 |
-
def generate_chat_completion(
|
| 22 |
-
"""
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=0.95,
|
| 36 |
-
repetition_penalty=1.15,
|
| 37 |
-
do_sample=True
|
| 38 |
-
)
|
| 39 |
-
response = output[0]['generated_text'].replace(prompt, "").strip()
|
| 40 |
-
return message_history + [response]
|
| 41 |
-
except Exception as e:
|
| 42 |
-
return message_history + [f"[Error] {str(e)}"]
|
| 43 |
-
|
| 44 |
-
# Gradio Chat Interface
|
| 45 |
-
chat_interface = gr.ChatInterface(
|
| 46 |
-
fn=generate_chat_completion,
|
| 47 |
-
title="Mistral-7B Chat",
|
| 48 |
-
description="Powered by Hugging Face Transformers",
|
| 49 |
-
retry_btn="Retry",
|
| 50 |
-
undo_btn="Undo",
|
| 51 |
-
clear_btn="Clear"
|
| 52 |
-
)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
import os
|
| 5 |
|
| 6 |
+
# Use safe float32 for CPU compatibility
|
| 7 |
+
torch_dtype = torch.float32
|
| 8 |
|
| 9 |
+
# Configure cache directory
|
| 10 |
+
os.environ['HF_HOME'] = '/tmp/cache'
|
| 11 |
|
| 12 |
# Load the model pipeline
|
| 13 |
generator = pipeline(
|
| 14 |
"text-generation",
|
| 15 |
+
model="mistralai/Mistral-7B-Instruct-v0.2",
|
| 16 |
+
device=0 if torch.cuda.is_available() else -1,
|
| 17 |
+
torch_dtype=torch_dtype
|
|
|
|
| 18 |
)
|
| 19 |
|
| 20 |
+
def generate_chat_completion(message, history):
|
| 21 |
+
"""Basic chatbot for Gradio interface"""
|
| 22 |
+
prompt = f"User: {message}\nAssistant:"
|
| 23 |
+
output = generator(
|
| 24 |
+
prompt,
|
| 25 |
+
max_new_tokens=512,
|
| 26 |
+
temperature=0.8,
|
| 27 |
+
top_p=0.95,
|
| 28 |
+
repetition_penalty=1.15,
|
| 29 |
+
do_sample=True
|
| 30 |
+
)
|
| 31 |
+
response = output[0]['generated_text'].replace(prompt, "").strip()
|
| 32 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
gr.ChatInterface(fn=generate_chat_completion,
|
| 35 |
+
title="Mistral Chatbot",
|
| 36 |
+
description="Chat with Mistral-7B",
|
| 37 |
+
retry_btn="Retry",
|
| 38 |
+
undo_btn="Undo",
|
| 39 |
+
clear_btn="Clear").launch()
|