ranggafermata commited on
Commit
eafb42b
·
verified ·
1 Parent(s): b89a989

Update backend/app.py

Browse files
Files changed (1) hide show
  1. backend/app.py +21 -126
backend/app.py CHANGED
@@ -1,145 +1,40 @@
1
- from flask import Flask, request, Response, jsonify
2
  from flask_cors import CORS
3
  from PIL import Image
4
  import torch
5
  from transformers import AutoProcessor, BlipForConditionalGeneration
6
- from llama_cpp import Llama
7
- import json
8
- from tavily import TavilyClient
9
- import os
10
- from dotenv import load_dotenv
11
-
12
- load_dotenv()
13
-
14
- TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
15
 
16
  app = Flask(__name__)
17
- CORS(app, resources={
18
- r"/*": {"origins": "*"} # Use a more permissive CORS for cloud deployment
19
- })
20
-
21
  device = "cuda" if torch.cuda.is_available() else "cpu"
22
- vision_processor, vision_model, llm, tavily_client = None, None, None, None
23
-
24
- # --- Load Models ---
25
- print("--- F-P-U-I --- Attempting to load models...")
26
 
27
  try:
28
  vision_processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
29
  vision_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large").to(device)
30
- print("BLIP Vision model loaded successfully.")
31
- except Exception as e:
32
- print(f"Error loading Vision model: {e}")
33
-
34
- try:
35
- llm = Llama.from_pretrained(
36
- repo_id="ranggafermata/Effort-1",
37
- filename="EffortQ43B.gguf",
38
- n_ctx=2048,
39
- n_gpu_layers=-1,
40
- verbose=False,
41
- chat_format="llama-3"
42
- )
43
- print("--- F-P-U-I --- Effort-1 text model loaded successfully.")
44
  except Exception as e:
45
- print(f"--- F-P-U-I --- CRITICAL ERROR loading Effort-1 model: {e}")
46
-
47
- try:
48
- tavily_client = TavilyClient(api_key=TAVILY_API_KEY)
49
- print("--- F-P-U-I --- Tavily client initialized successfully.")
50
- except Exception as e:
51
- print(f"--- F-P-U-I --- CRITICAL ERROR initializing Tavily client: {e}")
52
-
53
- @app.route("/research", methods=["POST"])
54
- def research():
55
-
56
- global tavily_client
57
-
58
- if not tavily_client:
59
- return jsonify({"error": "Tavily client not available"}), 500
60
-
61
- data = request.get_json()
62
- task = data.get("task")
63
- query = data.get("query")
64
 
65
- if not task or not query:
66
- return jsonify({"error": "Missing task or query"}), 400
67
-
68
- try:
69
- if task == 'search':
70
- results = tavily_client.search(query=query, search_depth="advanced")
71
- elif task == 'extract':
72
- results = tavily_client.extract(urls=[query])
73
- else:
74
- return jsonify({"error": "Invalid task"}), 400
75
-
76
- return jsonify(results)
77
-
78
- except Exception as e:
79
- print(f"Error during Tavily research: {e}")
80
-
81
- return jsonify({"error": str(e)}), 500
82
-
83
-
84
- # --- Main Endpoint ---
85
- @app.route("/completion", methods=["POST"])
86
- def completion():
87
- global llm, vision_model, vision_processor # Declare usage of globals
88
 
89
- prompt = request.form.get("prompt", "")
90
- history_json = request.form.get("history", "[]")
91
  image_file = request.files.get("image")
92
- model_choice = request.form.get("model_choice", "effort-1")
93
-
94
-
95
- pil_image = None
96
- if image_file:
97
- try:
98
- pil_image = Image.open(image_file.stream).convert("RGB")
99
- except Exception as e:
100
- print(f"Error opening image file: {e}")
101
 
102
  try:
103
- chat_history = json.loads(history_json)
104
- except json.JSONDecodeError:
105
- chat_history = []
106
-
107
- def generate_stream(user_prompt, image_obj, history, choice):
108
- if image_obj:
109
- if vision_model and vision_processor:
110
- try:
111
- inputs = (vision_processor(images=image_obj, text=user_prompt, return_tensors="pt").to(device) if user_prompt else vision_processor(images=image_obj, return_tensors="pt").to(device))
112
- output = vision_model.generate(**inputs, max_new_tokens=50)
113
- caption = vision_processor.decode(output[0], skip_special_tokens=True).strip()
114
- yield f"data: {json.dumps({'content': caption})}\n\n"
115
- except Exception as e:
116
- print(f"Error processing image: {e}")
117
- yield f"data: {json.dumps({'content': 'Sorry, I had trouble reading that image.'})}\n\n"
118
- else:
119
- yield f"data: {json.dumps({'content': 'Vision model not available.'})}\n\n"
120
-
121
- else:
122
- # --- Text Path ---
123
-
124
- if llm:
125
- try:
126
- system_message = {"role": "system", "content": "You are a helpful and brilliant AI assistant named Effort."}
127
- messages = [system_message] + history + [{"role": "user", "content": user_prompt}]
128
-
129
- print(f"Sending {len(messages)} messages to the Effort-1 model.")
130
-
131
- stream = llm.create_chat_completion(messages=messages, max_tokens=1024, temperature=0.7, stream=True)
132
- for output in stream:
133
- token = output["choices"][0]["delta"].get("content", "")
134
- if token:
135
- yield f"data: {json.dumps({'content': token})}\n\n"
136
- except Exception as e:
137
- print(f"Error during text generation: {e}")
138
- yield f"data: {json.dumps({'content': 'I encountered an error.'})}\n\n"
139
- else:
140
- yield f"data: {json.dumps({'content': 'Requested text model not available.'})}\n\n"
141
-
142
- return Response(generate_stream(prompt, pil_image, chat_history, model_choice), mimetype="text-event-stream")
143
 
144
  if __name__ == "__main__":
145
- app.run(host="0.0.0.0", port=8080)
 
1
+ from flask import Flask, request, jsonify
2
  from flask_cors import CORS
3
  from PIL import Image
4
  import torch
5
  from transformers import AutoProcessor, BlipForConditionalGeneration
 
 
 
 
 
 
 
 
 
6
 
7
  app = Flask(__name__)
8
+ CORS(app, resources={r"/*": {"origins": "*"}})
 
 
 
9
  device = "cuda" if torch.cuda.is_available() else "cpu"
10
+ vision_processor, vision_model = None, None
 
 
 
11
 
12
  try:
13
  vision_processor = AutoProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
14
  vision_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large").to(device)
15
+ print("--- VISION SERVICE --- BLIP Vision model loaded successfully.")
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  except Exception as e:
17
+ print(f"--- VISION SERVICE --- CRITICAL ERROR loading Vision model: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ @app.route("/describe_image", methods=["POST"])
20
+ def describe_image():
21
+ if not vision_model:
22
+ return jsonify({"error": "Vision model not available."}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ user_prompt = request.form.get("prompt", "")
 
25
  image_file = request.files.get("image")
26
+ if not image_file:
27
+ return jsonify({"error": "No image file found."}), 400
 
 
 
 
 
 
 
28
 
29
  try:
30
+ image_obj = Image.open(image_file.stream).convert("RGB")
31
+ inputs = (vision_processor(images=image_obj, text=user_prompt, return_tensors="pt").to(device) if user_prompt else vision_processor(images=image_obj, return_tensors="pt").to(device))
32
+ output = vision_model.generate(**inputs, max_new_tokens=50)
33
+ caption = vision_processor.decode(output[0], skip_special_tokens=True).strip()
34
+ return jsonify({"content": caption})
35
+ except Exception as e:
36
+ print(f"Error processing image: {e}")
37
+ return jsonify({"error": "Sorry, I had trouble processing that image."}), 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  if __name__ == "__main__":
40
+ app.run(host="0.0.0.0", port=8081) # Use a different port for local testing