ghosthets commited on
Commit
9239500
·
verified ·
1 Parent(s): 0eebe19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -13
app.py CHANGED
@@ -1,18 +1,41 @@
1
- from fastapi import FastAPI
2
- from pydantic import BaseModel
3
  from transformers import pipeline
 
4
 
5
- ai = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-beta", max_new_tokens=200)
6
- app = FastAPI()
7
 
8
- class Msg(BaseModel):
9
- message: str
 
 
 
 
10
 
11
- @app.post("/chat")
12
- def chat(msg: Msg):
 
 
 
 
 
 
 
 
 
 
13
  try:
14
- output = ai(msg.message)[0]["generated_text"]
15
- return {"reply": output}
16
- except:
17
- return {"reply": "ERROR"}
18
-
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
 
2
  from transformers import pipeline
3
+ import torch
4
 
5
+ app = Flask(__name__)
 
6
 
7
+ # ===========================
8
+ # LOAD LOCAL/FREE MODEL (HF TOKEN NAHI CHAHIYE)
9
+ # ===========================
10
+ # Apna model ID yaha daal sakte ho, ya HuggingFace free model
11
+ model_id = "HuggingFaceH4/zephyr-7b-beta" # Example free model
12
+ print("🔄 Loading model...")
13
 
14
+ # CPU/GPU device set
15
+ device = 0 if torch.cuda.is_available() else -1
16
+
17
+ # pipeline me device=-1 -> CPU, device=0 -> GPU
18
+ ai = pipeline("text-generation", model=model_id, max_new_tokens=200, device=device)
19
+ print("✅ Model loaded!")
20
+
21
+ # ===========================
22
+ # CHAT API
23
+ # ===========================
24
+ @app.route('/chat', methods=['POST'])
25
+ def chat():
26
  try:
27
+ data = request.get_json()
28
+ msg = data.get("message", "")
29
+ if not msg:
30
+ return jsonify({"error": "No message sent"}), 400
31
+
32
+ output = ai(msg)[0]["generated_text"]
33
+ return jsonify({"reply": output})
34
+ except Exception as e:
35
+ return jsonify({"error": str(e)}), 500
36
+
37
+ # ===========================
38
+ # RUN SERVER
39
+ # ===========================
40
+ if __name__ == "__main__":
41
+ app.run(host='0.0.0.0', port=7860)