Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,129 +1,26 @@
|
|
| 1 |
-
#
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
-
import torch
|
| 7 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 8 |
-
from flask import Flask, request, jsonify
|
| 9 |
-
|
| 10 |
-
app = Flask(__name__)
|
| 11 |
-
|
| 12 |
-
# Choose a lightweight open model that can run on limited hardware
|
| 13 |
-
# Options include:
|
| 14 |
-
# - GPT2-small (if you have ~2GB RAM for the model)
|
| 15 |
-
# - Hugging Face's inference endpoints (cloud-based, some free tiers)
|
| 16 |
-
# - Models like DialoGPT-small, BLOOM-560M, or OPT-350M
|
| 17 |
-
|
| 18 |
-
# Configuration
|
| 19 |
-
MODEL_NAME = "EleutherAI/gpt-neo-125M" # A relatively small model, replace with your choice
|
| 20 |
-
USE_CLOUD_INFERENCE = True # Set to True to use Hugging Face's Inference API instead of local model
|
| 21 |
-
|
| 22 |
-
# Hugging Face API Token (sign up for free at huggingface.co)
|
| 23 |
-
HF_API_TOKEN = os.environ.get("HF_API_TOKEN", "") # Store your token as an environment variable for security
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
NORTHERN_AI is friendly, concise, and knowledgeable."""
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
if not USE_CLOUD_INFERENCE:
|
| 35 |
-
print("Loading model locally (requires sufficient RAM)...")
|
| 36 |
-
self.tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 37 |
-
# Load in 8-bit to reduce memory requirements
|
| 38 |
-
self.model = AutoModelForCausalLM.from_pretrained(
|
| 39 |
-
MODEL_NAME,
|
| 40 |
-
torch_dtype=torch.float16,
|
| 41 |
-
low_cpu_mem_usage=True,
|
| 42 |
-
device_map="auto"
|
| 43 |
-
)
|
| 44 |
-
else:
|
| 45 |
-
print("Using cloud inference API (minimal RAM required)...")
|
| 46 |
-
# For cloud inference, we'll just need the API endpoint
|
| 47 |
-
from huggingface_hub import InferenceClient
|
| 48 |
-
self.client = InferenceClient(token=HF_API_TOKEN)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
# Use Hugging Face's Inference API
|
| 55 |
-
response = self.client.text_generation(
|
| 56 |
-
prompt,
|
| 57 |
-
model=MODEL_NAME,
|
| 58 |
-
max_new_tokens=150,
|
| 59 |
-
temperature=0.7,
|
| 60 |
-
top_p=0.95,
|
| 61 |
-
repetition_penalty=1.1
|
| 62 |
-
)
|
| 63 |
-
return response.strip()
|
| 64 |
-
else:
|
| 65 |
-
# Local generation
|
| 66 |
-
inputs = self.tokenizer(prompt, return_tensors="pt").to(self.model.device)
|
| 67 |
-
with torch.no_grad():
|
| 68 |
-
output = self.model.generate(
|
| 69 |
-
**inputs,
|
| 70 |
-
max_new_tokens=150,
|
| 71 |
-
temperature=0.7,
|
| 72 |
-
top_p=0.95,
|
| 73 |
-
repetition_penalty=1.1
|
| 74 |
-
)
|
| 75 |
-
return self.tokenizer.decode(output[0], skip_special_tokens=True).split("NORTHERN_AI:")[-1].strip()
|
| 76 |
-
|
| 77 |
-
# Initialize the AI assistant
|
| 78 |
-
northern_ai = NorthernAI()
|
| 79 |
-
|
| 80 |
-
@app.route('/api/chat', methods=['POST'])
|
| 81 |
-
def chat():
|
| 82 |
-
data = request.json
|
| 83 |
-
user_message = data.get('message', '')
|
| 84 |
-
response = northern_ai.generate_response(user_message)
|
| 85 |
-
return jsonify({"response": response})
|
| 86 |
-
|
| 87 |
-
@app.route('/')
|
| 88 |
-
def home():
|
| 89 |
-
return """
|
| 90 |
-
<html>
|
| 91 |
-
<head><title>NORTHERN_AI by AR.BALTEE</title></head>
|
| 92 |
-
<body>
|
| 93 |
-
<h1>Welcome to NORTHERN_AI</h1>
|
| 94 |
-
<form id="chat-form">
|
| 95 |
-
<input type="text" id="user-input" placeholder="Ask NORTHERN_AI something...">
|
| 96 |
-
<button type="submit">Send</button>
|
| 97 |
-
</form>
|
| 98 |
-
<div id="chat-history"></div>
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
chatHistory.innerHTML += `<p><strong>You:</strong> ${message}</p>`;
|
| 110 |
-
|
| 111 |
-
// Get AI response
|
| 112 |
-
const response = await fetch('/api/chat', {
|
| 113 |
-
method: 'POST',
|
| 114 |
-
headers: {'Content-Type': 'application/json'},
|
| 115 |
-
body: JSON.stringify({message})
|
| 116 |
-
});
|
| 117 |
-
|
| 118 |
-
const data = await response.json();
|
| 119 |
-
chatHistory.innerHTML += `<p><strong>NORTHERN_AI:</strong> ${data.response}</p>`;
|
| 120 |
-
});
|
| 121 |
-
</script>
|
| 122 |
-
</body>
|
| 123 |
-
</html>
|
| 124 |
-
"""
|
| 125 |
|
| 126 |
-
if __name__ == '__main__':
|
| 127 |
-
# Use the PORT environment variable provided by most free hosting services
|
| 128 |
-
port = int(os.environ.get("PORT", 5000))
|
| 129 |
-
app.run(host='0.0.0.0', port=port)
|
|
|
|
| 1 |
+
# NOimport os
|
| 2 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def generate_response(message):
|
| 5 |
+
# Simple response for testing
|
| 6 |
+
return f"NORTHERN_AI: Thank you for your message: '{message}'"
|
|
|
|
| 7 |
|
| 8 |
+
# Create Gradio interface
|
| 9 |
+
with gr.Blocks() as demo:
|
| 10 |
+
gr.Markdown("# NORTHERN_AI by AR.BALTEE")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
with gr.Row():
|
| 13 |
+
with gr.Column():
|
| 14 |
+
message = gr.Textbox(label="Your message")
|
| 15 |
+
submit = gr.Button("Send")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
with gr.Column():
|
| 18 |
+
output = gr.Textbox(label="NORTHERN_AI Response")
|
| 19 |
+
|
| 20 |
+
submit.click(generate_response, inputs=message, outputs=output)
|
| 21 |
+
|
| 22 |
+
# Launch the app
|
| 23 |
+
if __name__ == "__main__":
|
| 24 |
+
demo.launch()RTHERN_AI
|
| 25 |
+
# Created by AR.BALTEE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|