Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -57,11 +57,8 @@ def setup_seeds(config):
|
|
57 |
# ========================================
|
58 |
|
59 |
SHARED_UI_WARNING = f'''### [NOTE] It is possible that you are waiting in a lengthy queue.
|
60 |
-
|
61 |
You can duplicate and use it with a paid private GPU.
|
62 |
-
|
63 |
<a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/Vision-CAIR/minigpt4?duplicate=true"><img style="margin-top:0;margin-bottom:0" src="https://huggingface.co/datasets/huggingface/badges/raw/main/duplicate-this-space-xl-dark.svg" alt="Duplicate Space"></a>
|
64 |
-
|
65 |
Alternatively, you can also use the demo on our [project page](https://minigpt-4.github.io).
|
66 |
'''
|
67 |
|
@@ -95,33 +92,54 @@ class Item(BaseModel):
|
|
95 |
gr_img: UploadFile = File(..., description="Image file")
|
96 |
text_input: str = None
|
97 |
|
|
|
|
|
|
|
|
|
|
|
98 |
@app.get("/")
|
99 |
async def root():
|
100 |
return RedirectResponse(url="/docs")
|
101 |
|
102 |
-
|
103 |
-
|
|
|
104 |
file: UploadFile = File(...),
|
105 |
-
prompt: str = Form(...),
|
106 |
):
|
107 |
-
chat_state = CONV_VISION.copy()
|
108 |
-
img_list = []
|
109 |
-
chatbot=[]
|
110 |
pil_image = Image.open(io.BytesIO(await file.read()))
|
111 |
chat.upload_img(pil_image, chat_state, img_list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
chat.ask(prompt, chat_state)
|
|
|
113 |
chatbot = chatbot + [[prompt, None]]
|
114 |
llm_message = chat.answer(conv=chat_state, img_list=img_list, max_new_tokens=300, num_beams=1, temperature=1,
|
115 |
max_length=2000)[0]
|
116 |
chatbot[-1][1] = llm_message
|
117 |
return chatbot
|
118 |
|
|
|
|
|
119 |
|
120 |
-
|
121 |
-
#
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
|
|
126 |
|
127 |
|
|
|
|
|
|
|
|
57 |
# ========================================
|
58 |
|
59 |
SHARED_UI_WARNING = f'''### [NOTE] It is possible that you are waiting in a lengthy queue.
|
|
|
60 |
You can duplicate and use it with a paid private GPU.
|
|
|
61 |
<a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/Vision-CAIR/minigpt4?duplicate=true"><img style="margin-top:0;margin-bottom:0" src="https://huggingface.co/datasets/huggingface/badges/raw/main/duplicate-this-space-xl-dark.svg" alt="Duplicate Space"></a>
|
|
|
62 |
Alternatively, you can also use the demo on our [project page](https://minigpt-4.github.io).
|
63 |
'''
|
64 |
|
|
|
92 |
gr_img: UploadFile = File(..., description="Image file")
|
93 |
text_input: str = None
|
94 |
|
95 |
+
chat_state = CONV_VISION.copy()
|
96 |
+
img_list = []
|
97 |
+
chatbot = []
|
98 |
+
|
99 |
+
|
100 |
@app.get("/")
|
101 |
async def root():
|
102 |
return RedirectResponse(url="/docs")
|
103 |
|
104 |
+
|
105 |
+
@app.post("/upload_img/")
|
106 |
+
async def upload_img(
|
107 |
file: UploadFile = File(...),
|
|
|
108 |
):
|
|
|
|
|
|
|
109 |
pil_image = Image.open(io.BytesIO(await file.read()))
|
110 |
chat.upload_img(pil_image, chat_state, img_list)
|
111 |
+
return {"message": "image uploaded successfully."}
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
@app.post("/process/")
|
118 |
+
async def process_item(
|
119 |
+
prompt: str = Form(...),
|
120 |
+
):
|
121 |
+
|
122 |
+
|
123 |
chat.ask(prompt, chat_state)
|
124 |
+
global chatbot
|
125 |
chatbot = chatbot + [[prompt, None]]
|
126 |
llm_message = chat.answer(conv=chat_state, img_list=img_list, max_new_tokens=300, num_beams=1, temperature=1,
|
127 |
max_length=2000)[0]
|
128 |
chatbot[-1][1] = llm_message
|
129 |
return chatbot
|
130 |
|
131 |
+
@app.post("/reset/")
|
132 |
+
async def reset(
|
133 |
|
134 |
+
):
|
135 |
+
global chat_state, img_list # Use global keyword to reassign
|
136 |
+
img_list = []
|
137 |
+
if chat_state is not None:
|
138 |
+
chat_state.messages = []
|
139 |
+
if img_list is not None:
|
140 |
+
img_list = []
|
141 |
|
142 |
|
143 |
+
if __name__ == "__main__":
|
144 |
+
# Run the FastAPI app with Uvicorn
|
145 |
+
uvicorn.run("main:app", host="0.0.0.0", port=7860)
|