Erfan11 commited on
Commit
e96679f
1 Parent(s): f146cbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -40
app.py CHANGED
@@ -1,41 +1,50 @@
 
 
1
  import os
2
- from flask import Flask, request, jsonify
3
- from transformers import AutoModelForCausalLM, AutoTokenizer
4
- import yaml
5
-
6
- # Load environment variables
7
- api_key = os.getenv('HF_API_KEY')
8
- model_path = os.getenv('MODEL_PATH')
9
-
10
- # Initialize Flask app
11
- app = Flask(__name__)
12
-
13
- # Load configuration
14
- with open('config.yaml', 'r') as file:
15
- config = yaml.safe_load(file)
16
-
17
- # Load the model and tokenizer
18
- def load_model():
19
- tokenizer = AutoTokenizer.from_pretrained(model_path)
20
- model = AutoModelForCausalLM.from_pretrained(model_path)
21
- return model, tokenizer
22
-
23
- model, tokenizer = load_model()
24
-
25
- def generate_text(prompt):
26
- inputs = tokenizer(prompt, return_tensors="pt")
27
- outputs = model.generate(inputs['input_ids'])
28
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
29
-
30
- @app.route('/generate', methods=['POST'])
31
- def generate():
32
- data = request.get_json()
33
- prompt = data.get('prompt')
34
- if prompt:
35
- response_text = generate_text(prompt)
36
- return jsonify({"response": response_text})
37
- else:
38
- return jsonify({"error": "No prompt provided"})
39
-
40
- if __name__ == '__main__':
41
- app.run(debug=True)
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException, UploadFile, File
2
+ from transformers import pipeline
3
  import os
4
+ from typing import List
5
+ from pydantic import BaseModel
6
+
7
+ app = FastAPI()
8
+
9
+ # Initialize the AI model
10
+ model_name = "gpt-3.5-turbo"
11
+ generator = pipeline('text-generation', model=model_name)
12
+
13
+ class FileUpdate(BaseModel):
14
+ filename: str
15
+ content: str
16
+
17
+ @app.post("/generate")
18
+ def generate_text(prompt: str):
19
+ try:
20
+ result = generator(prompt, max_length=100)
21
+ return {"response": result[0]['generated_text']}
22
+ except Exception as e:
23
+ raise HTTPException(status_code=500, detail=str(e))
24
+
25
+ @app.post("/upload")
26
+ async def upload_file(file: UploadFile = File(...)):
27
+ try:
28
+ with open(f"./files/{file.filename}", "wb") as f:
29
+ content = await file.read()
30
+ f.write(content)
31
+ return {"filename": file.filename}
32
+ except Exception as e:
33
+ raise HTTPException(status_code=500, detail=str(e))
34
+
35
+ @app.post("/update")
36
+ def update_file(file_update: FileUpdate):
37
+ try:
38
+ with open(f"./files/{file_update.filename}", "w") as f:
39
+ f.write(file_update.content)
40
+ return {"status": "success"}
41
+ except Exception as e:
42
+ raise HTTPException(status_code=500, detail=str(e))
43
+
44
+ @app.get("/files")
45
+ def list_files():
46
+ try:
47
+ files = os.listdir("./files")
48
+ return {"files": files}
49
+ except Exception as e:
50
+ raise HTTPException(status_code=500, detail=str(e))