Spaces:
Runtime error
Runtime error
File size: 1,710 Bytes
2b12b08 18e3582 c0fe323 efdd63d 29cdb01 2b12b08 d5c6c7d 29cdb01 2b12b08 6cf41e9 2b12b08 18e3582 c0fe323 18e3582 c0fe323 2b12b08 |
1 2 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import gradio as gr
from transformers import pipeline
import torch
import os
# Configure cache to avoid space limitations
os.environ['HF_HOME'] = '/tmp/cache'
# Use a reliable LLM hosted by Hugging Face
MODEL_NAME = "mistralai/Mistral-7B-Instruct-v0.2"
# Load the model pipeline
generator = pipeline(
"text-generation",
model=MODEL_NAME,
device_map="auto",
torch_dtype=torch.bfloat16,
max_new_tokens=560
)
def generate_chat_completion(message_history, max_tokens=560, temperature=0.8):
"""Generate assistant response from chat message history"""
try:
# If using Gradio chat format (list of tuples), convert to role-content dicts
messages = [{"role": "user", "content": msg} if i % 2 == 0 else {"role": "assistant", "content": msg}
for i, msg in enumerate(message_history)]
prompt = "\n".join([f"{m['role'].capitalize()}: {m['content']}" for m in messages])
prompt += "\nAssistant:"
output = generator(
prompt,
max_new_tokens=max_tokens,
temperature=temperature,
top_p=0.95,
repetition_penalty=1.15,
do_sample=True
)
response = output[0]['generated_text'].replace(prompt, "").strip()
return message_history + [response]
except Exception as e:
return message_history + [f"[Error] {str(e)}"]
# Gradio Chat Interface
chat_interface = gr.ChatInterface(
fn=generate_chat_completion,
title="Mistral-7B Chat",
description="Powered by Hugging Face Transformers",
retry_btn="Retry",
undo_btn="Undo",
clear_btn="Clear"
)
if __name__ == "__main__":
chat_interface.launch()
|