Spaces:
Runtime error
Runtime error
Praveen0309
commited on
Commit
•
a6ca086
1
Parent(s):
9732f39
final_commit
Browse files- __pycache__/main.cpython-39.pyc +0 -0
- main.py +68 -37
- static/script.js +2 -2
__pycache__/main.cpython-39.pyc
CHANGED
Binary files a/__pycache__/main.cpython-39.pyc and b/__pycache__/main.cpython-39.pyc differ
|
|
main.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
# Import necessary libraries
|
2 |
-
from flask import Flask, render_template, request, jsonify
|
3 |
from PIL import Image
|
4 |
from peft import PeftModel
|
5 |
from PIL import Image
|
@@ -7,9 +6,12 @@ import torch
|
|
7 |
from transformers import AutoProcessor, LlavaForConditionalGeneration, BitsAndBytesConfig
|
8 |
from deep_translator import GoogleTranslator
|
9 |
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
10 |
-
from fastapi import FastAPI
|
11 |
from fastapi.staticfiles import StaticFiles
|
12 |
-
from fastapi.responses import FileResponse
|
|
|
|
|
|
|
13 |
import warnings
|
14 |
# from flask import Flask
|
15 |
|
@@ -20,20 +22,26 @@ app = FastAPI()
|
|
20 |
|
21 |
warnings.filterwarnings('ignore')
|
22 |
|
23 |
-
|
|
|
|
|
|
|
24 |
|
25 |
|
|
|
26 |
|
27 |
-
model_id = "HuggingFaceH4/vsft-llava-1.5-7b-hf-trl"
|
28 |
-
quantization_config = BitsAndBytesConfig(load_in_4bit=True)
|
29 |
-
base_model = LlavaForConditionalGeneration.from_pretrained(model_id, quantization_config=quantization_config, torch_dtype=torch.float16)
|
30 |
|
31 |
-
# Load the PEFT Lora adapter
|
32 |
-
peft_lora_adapter_path = "Praveen0309/llava-1.5-7b-hf-ft-mix-vsft-3"
|
33 |
-
peft_lora_adapter = PeftModel.from_pretrained(base_model, peft_lora_adapter_path, adapter_name="lora_adapter")
|
34 |
-
base_model.load_adapter(peft_lora_adapter_path, adapter_name="lora_adapter")
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
# model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
|
38 |
# tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
|
39 |
|
@@ -124,43 +132,66 @@ def facebook_response(url, input_sentence):
|
|
124 |
|
125 |
|
126 |
image_cache = {}
|
127 |
-
@app.post("/upload
|
128 |
-
def upload_file():
|
129 |
try:
|
130 |
-
file = request.files['file']
|
131 |
if file.filename.endswith('.jpg'):
|
132 |
-
|
|
|
|
|
133 |
# Store the image in cache (replace with a more suitable storage approach)
|
134 |
image_cache['image'] = image
|
135 |
# print("Processing complete. Image stored in cache.")
|
136 |
-
return
|
137 |
else:
|
138 |
-
|
|
|
139 |
except Exception as e:
|
140 |
-
|
141 |
-
return
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
try:
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
|
|
160 |
except Exception as e:
|
161 |
return f"Error: {str(e)}"
|
162 |
|
163 |
|
|
|
|
|
|
|
|
|
|
|
164 |
# Run the Flask app
|
165 |
# if __name__ == "__main__":
|
166 |
app.run(debug = True)
|
|
|
1 |
# Import necessary libraries
|
|
|
2 |
from PIL import Image
|
3 |
from peft import PeftModel
|
4 |
from PIL import Image
|
|
|
6 |
from transformers import AutoProcessor, LlavaForConditionalGeneration, BitsAndBytesConfig
|
7 |
from deep_translator import GoogleTranslator
|
8 |
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
9 |
+
from fastapi import FastAPI, Query, UploadFile, File, HTTPException
|
10 |
from fastapi.staticfiles import StaticFiles
|
11 |
+
from fastapi.responses import FileResponse, JSONResponse
|
12 |
+
from io import BytesIO
|
13 |
+
|
14 |
+
|
15 |
import warnings
|
16 |
# from flask import Flask
|
17 |
|
|
|
22 |
|
23 |
warnings.filterwarnings('ignore')
|
24 |
|
25 |
+
|
26 |
+
@app.get('/echo/')
|
27 |
+
async def echo(query_param: str):
|
28 |
+
return {"response": query_param}
|
29 |
|
30 |
|
31 |
+
# app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
32 |
|
|
|
|
|
|
|
33 |
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
# model_id = "HuggingFaceH4/vsft-llava-1.5-7b-hf-trl"
|
36 |
+
# quantization_config = BitsAndBytesConfig(load_in_4bit=True)
|
37 |
+
# base_model = LlavaForConditionalGeneration.from_pretrained(model_id, quantization_config=quantization_config, torch_dtype=torch.float16)
|
38 |
+
|
39 |
+
# # Load the PEFT Lora adapter
|
40 |
+
# peft_lora_adapter_path = "Praveen0309/llava-1.5-7b-hf-ft-mix-vsft-3"
|
41 |
+
# peft_lora_adapter = PeftModel.from_pretrained(base_model, peft_lora_adapter_path, adapter_name="lora_adapter")
|
42 |
+
# base_model.load_adapter(peft_lora_adapter_path, adapter_name="lora_adapter")
|
43 |
+
|
44 |
+
# processor = AutoProcessor.from_pretrained("HuggingFaceH4/vsft-llava-1.5-7b-hf-trl")
|
45 |
# model = M2M100ForConditionalGeneration.from_pretrained("facebook/m2m100_418M")
|
46 |
# tokenizer = M2M100Tokenizer.from_pretrained("facebook/m2m100_418M")
|
47 |
|
|
|
132 |
|
133 |
|
134 |
image_cache = {}
|
135 |
+
@app.post("/upload")
|
136 |
+
async def upload_file(file: UploadFile = File(...)):
|
137 |
try:
|
138 |
+
# file = request.files['file']
|
139 |
if file.filename.endswith('.jpg'):
|
140 |
+
contents = await file.read()
|
141 |
+
image = Image.open(BytesIO(contents))
|
142 |
+
# image = Image.open(file.stream)
|
143 |
# Store the image in cache (replace with a more suitable storage approach)
|
144 |
image_cache['image'] = image
|
145 |
# print("Processing complete. Image stored in cache.")
|
146 |
+
return JSONResponse(content={'status': 'success'})
|
147 |
else:
|
148 |
+
# print("dfsd")
|
149 |
+
return JSONResponse(content={'status': 'error', 'message': 'Uploaded file is not a jpg image.'})
|
150 |
except Exception as e:
|
151 |
+
print(e)
|
152 |
+
return JSONResponse(content={'status': 'error', 'message': str(e)})
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
# @app.get("/get/")
|
157 |
+
# async def get_items(msg: str):
|
158 |
+
|
159 |
+
# try:
|
160 |
+
# print( msg )
|
161 |
+
# if 'image' in image_cache:
|
162 |
+
# image = image_cache['image']
|
163 |
+
# # print(image)
|
164 |
+
# query = request.args.get('msg')
|
165 |
+
# output = query
|
166 |
+
# # output = google_response(image, query)
|
167 |
+
# return output
|
168 |
+
# else:
|
169 |
+
# return "Please upload an image to continue"
|
170 |
+
# except Exception as e:
|
171 |
+
# return f"Error: {str(e)}"
|
172 |
+
|
173 |
+
@app.get("/get")
|
174 |
+
async def get_items(msg: str):
|
175 |
try:
|
176 |
+
# print(msg)
|
177 |
+
if 'image' in image_cache:
|
178 |
+
image = image_cache['image']
|
179 |
+
# print(image)
|
180 |
+
output = msg # You can directly use the `msg` parameter here
|
181 |
+
# output = google_response(image, msg) # Uncomment if you have a function named google_response
|
182 |
+
return output
|
183 |
+
else:
|
184 |
+
# return msg
|
185 |
+
return "Please upload an image to continue"
|
186 |
except Exception as e:
|
187 |
return f"Error: {str(e)}"
|
188 |
|
189 |
|
190 |
+
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
191 |
+
@app.get("/")
|
192 |
+
def home() -> FileResponse:
|
193 |
+
return FileResponse(path="/app/static/index.html")
|
194 |
+
|
195 |
# Run the Flask app
|
196 |
# if __name__ == "__main__":
|
197 |
app.run(debug = True)
|
static/script.js
CHANGED
@@ -3,7 +3,7 @@ $(document).ready(function(){
|
|
3 |
e.preventDefault();
|
4 |
$('#uploadStatus').html('<p>Status: Processing...</p>');
|
5 |
$.ajax({
|
6 |
-
url: "/upload
|
7 |
type: 'POST',
|
8 |
data: new FormData(this),
|
9 |
contentType: false,
|
@@ -64,7 +64,7 @@ $(document).ready(function(){
|
|
64 |
message.draw();
|
65 |
|
66 |
// Call getResponse() to get the chatbot's response
|
67 |
-
$.get("/get
|
68 |
// Draw bot message with bot-message class
|
69 |
var botMessage = new Message({
|
70 |
text: data,
|
|
|
3 |
e.preventDefault();
|
4 |
$('#uploadStatus').html('<p>Status: Processing...</p>');
|
5 |
$.ajax({
|
6 |
+
url: "/upload",
|
7 |
type: 'POST',
|
8 |
data: new FormData(this),
|
9 |
contentType: false,
|
|
|
64 |
message.draw();
|
65 |
|
66 |
// Call getResponse() to get the chatbot's response
|
67 |
+
$.get("/get", { msg: text }).done(function(data) {
|
68 |
// Draw bot message with bot-message class
|
69 |
var botMessage = new Message({
|
70 |
text: data,
|