Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,53 @@
|
|
|
|
|
| 1 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 2 |
import torch
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
model_id = "dexcommunity/dex" # 👈 yahan apna model path do
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# Check device (GPU ya CPU)
|
| 12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
model.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
# Chat function
|
| 16 |
-
def ask_dex(prompt, max_length=256):
|
| 17 |
-
input_text = f"User: {prompt}\nDex:"
|
| 18 |
-
inputs = tokenizer(input_text, return_tensors="pt").to(device)
|
| 19 |
-
|
| 20 |
-
output = model.generate(
|
| 21 |
-
**inputs,
|
| 22 |
-
max_length=max_length,
|
| 23 |
-
do_sample=True,
|
| 24 |
-
top_k=50,
|
| 25 |
-
top_p=0.9,
|
| 26 |
-
temperature=0.7,
|
| 27 |
-
pad_token_id=tokenizer.eos_token_id
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 31 |
-
reply = decoded.split("Dex:")[-1].strip()
|
| 32 |
-
return reply
|
| 33 |
-
|
| 34 |
-
# Example use
|
| 35 |
if __name__ == "__main__":
|
| 36 |
-
|
| 37 |
-
user_input = input("You: ")
|
| 38 |
-
if user_input.lower() in ["exit", "quit"]:
|
| 39 |
-
break
|
| 40 |
-
response = ask_dex(user_input)
|
| 41 |
-
print("Dex:", response)
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
from huggingface_hub import login
|
| 4 |
import torch
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
app = Flask(__name__)
|
|
|
|
| 8 |
|
| 9 |
+
# ✅ Securely fetch HF Token from environment (invisible to users)
|
| 10 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 11 |
+
if not hf_token:
|
| 12 |
+
raise ValueError("HF_TOKEN is not set in environment variables!")
|
| 13 |
+
|
| 14 |
+
# 🔐 Authenticate
|
| 15 |
+
login(token=hf_token)
|
| 16 |
+
|
| 17 |
+
# 🔄 Load model from Hugging Face
|
| 18 |
+
model_id = "dexcommunity/dex"
|
| 19 |
+
print("🔄 Loading model...")
|
| 20 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token)
|
| 21 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, token=hf_token)
|
| 22 |
|
|
|
|
| 23 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 24 |
model.to(device)
|
| 25 |
+
print("✅ Model loaded!")
|
| 26 |
+
|
| 27 |
+
@app.route('/chat', methods=['POST'])
|
| 28 |
+
def chat():
|
| 29 |
+
try:
|
| 30 |
+
data = request.get_json()
|
| 31 |
+
msg = data.get("message", "")
|
| 32 |
+
if not msg:
|
| 33 |
+
return jsonify({"error": "No message sent"}), 400
|
| 34 |
+
|
| 35 |
+
prompt = f"User: {msg}\nDex:"
|
| 36 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 37 |
+
outputs = model.generate(
|
| 38 |
+
inputs.input_ids,
|
| 39 |
+
max_length=256,
|
| 40 |
+
do_sample=True,
|
| 41 |
+
top_k=50,
|
| 42 |
+
top_p=0.95,
|
| 43 |
+
temperature=0.7,
|
| 44 |
+
pad_token_id=tokenizer.eos_token_id
|
| 45 |
+
)
|
| 46 |
+
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 47 |
+
reply = text.split("Dex:")[-1].strip()
|
| 48 |
+
return jsonify({"reply": reply})
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return jsonify({"error": str(e)}), 500
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
if __name__ == "__main__":
|
| 53 |
+
app.run(host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|