Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import random | |
| # Configuration π οΈ | |
| model_name = "microsoft/phi-3-mini-4k-instruct" | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| # Load model with optimizations | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_name, | |
| torch_dtype=torch.float16 if device == "cuda" else torch.float32, | |
| device_map="auto", | |
| low_cpu_mem_usage=True | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained(model_name) | |
| # Safety tools π‘οΈ | |
| SAFE_RESPONSES = [ | |
| "Let's focus on positive tech projects! π±", | |
| "How about designing an eco-friendly robot? π€" | |
| ] | |
| def generate_response(message, history): | |
| # Convert history to new message format | |
| messages = [{"role": "user", "content": msg} for msg, _ in history] | |
| messages += [{"role": "assistant", "content": res} for _, res in history] | |
| # Safety check | |
| if any(word in message.lower() for word in ["violence", "hate", "gun"]): | |
| return random.choice(SAFE_RESPONSES) | |
| # Generate response | |
| inputs = tokenizer.apply_chat_template( | |
| [{"role": "user", "content": message}], | |
| return_tensors="pt" | |
| ).to(model.device) | |
| outputs = model.generate( | |
| inputs, | |
| max_new_tokens=256, | |
| temperature=0.7, | |
| do_sample=True | |
| ) | |
| return tokenizer.decode(outputs[0][inputs.shape[-1]:], skip_special_tokens=True) | |
| # Create Gradio interface with updated message format | |
| demo = gr.ChatInterface( | |
| generate_response, | |
| examples=[ | |
| "How to make a solar-powered robot?", | |
| "Python code for air quality sensor" | |
| ], | |
| title="π€ REACT Ethical AI Lab", | |
| chatbot=gr.Chatbot(height=500) # Removed invalid 'likeable' parameter | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| share=False | |
| ) |