Spaces:
Build error
Build error
Ajoute LLM et generation d'image
Browse files- .gitignore +6 -0
- app.py +60 -91
- requirements.txt +3 -1
.gitignore
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
bin
|
| 2 |
+
flagged
|
| 3 |
+
lib
|
| 4 |
+
lib64
|
| 5 |
+
pyvenv.cfg
|
| 6 |
+
share
|
app.py
CHANGED
|
@@ -1,104 +1,73 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import cv2
|
| 3 |
import requests
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
file_urls = [
|
| 9 |
-
'https://www.dropbox.com/s/b5g97xo901zb3ds/pothole_example.jpg?dl=1',
|
| 10 |
-
'https://www.dropbox.com/s/86uxlxxlm1iaexa/pothole_screenshot.png?dl=1',
|
| 11 |
-
'https://www.dropbox.com/s/7sjfwncffg8xej2/video_7.mp4?dl=1'
|
| 12 |
-
]
|
| 13 |
|
| 14 |
-
def download_file(url, save_name):
|
| 15 |
-
url = url
|
| 16 |
-
if not os.path.exists(save_name):
|
| 17 |
-
file = requests.get(url)
|
| 18 |
-
open(save_name, 'wb').write(file.content)
|
| 19 |
-
|
| 20 |
-
for i, url in enumerate(file_urls):
|
| 21 |
-
if 'mp4' in file_urls[i]:
|
| 22 |
-
download_file(
|
| 23 |
-
file_urls[i],
|
| 24 |
-
f"video.mp4"
|
| 25 |
-
)
|
| 26 |
-
else:
|
| 27 |
-
download_file(
|
| 28 |
-
file_urls[i],
|
| 29 |
-
f"image_{i}.jpg"
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
model = YOLO('best.pt')
|
| 33 |
-
path = [['image_0.jpg'], ['image_1.jpg']]
|
| 34 |
-
video_path = [['video.mp4']]
|
| 35 |
-
|
| 36 |
-
def show_preds_image(image_path):
|
| 37 |
-
image = cv2.imread(image_path)
|
| 38 |
-
outputs = model.predict(source=image_path)
|
| 39 |
-
results = outputs[0].cpu().numpy()
|
| 40 |
-
for i, det in enumerate(results.boxes.xyxy):
|
| 41 |
-
cv2.rectangle(
|
| 42 |
-
image,
|
| 43 |
-
(int(det[0]), int(det[1])),
|
| 44 |
-
(int(det[2]), int(det[3])),
|
| 45 |
-
color=(0, 0, 255),
|
| 46 |
-
thickness=2,
|
| 47 |
-
lineType=cv2.LINE_AA
|
| 48 |
-
)
|
| 49 |
-
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 50 |
-
|
| 51 |
-
inputs_image = [
|
| 52 |
-
gr.components.Image(type="filepath", label="Input Image"),
|
| 53 |
-
]
|
| 54 |
outputs_image = [
|
| 55 |
-
gr.components.Image(type="
|
|
|
|
| 56 |
]
|
|
|
|
| 57 |
interface_image = gr.Interface(
|
| 58 |
-
fn=
|
| 59 |
-
inputs=
|
| 60 |
outputs=outputs_image,
|
| 61 |
-
title="
|
| 62 |
-
examples=path,
|
| 63 |
-
cache_examples=False,
|
| 64 |
)
|
| 65 |
|
| 66 |
-
|
| 67 |
-
cap = cv2.VideoCapture(video_path)
|
| 68 |
-
while(cap.isOpened()):
|
| 69 |
-
ret, frame = cap.read()
|
| 70 |
-
if ret:
|
| 71 |
-
frame_copy = frame.copy()
|
| 72 |
-
outputs = model.predict(source=frame)
|
| 73 |
-
results = outputs[0].cpu().numpy()
|
| 74 |
-
for i, det in enumerate(results.boxes.xyxy):
|
| 75 |
-
cv2.rectangle(
|
| 76 |
-
frame_copy,
|
| 77 |
-
(int(det[0]), int(det[1])),
|
| 78 |
-
(int(det[2]), int(det[3])),
|
| 79 |
-
color=(0, 0, 255),
|
| 80 |
-
thickness=2,
|
| 81 |
-
lineType=cv2.LINE_AA
|
| 82 |
-
)
|
| 83 |
-
yield cv2.cvtColor(frame_copy, cv2.COLOR_BGR2RGB)
|
| 84 |
-
|
| 85 |
-
inputs_video = [
|
| 86 |
-
gr.components.Video(label="Input Video"),
|
| 87 |
-
|
| 88 |
-
]
|
| 89 |
-
outputs_video = [
|
| 90 |
-
gr.components.Image(label="Output Image"),
|
| 91 |
-
]
|
| 92 |
-
interface_video = gr.Interface(
|
| 93 |
-
fn=show_preds_video,
|
| 94 |
-
inputs=inputs_video,
|
| 95 |
-
outputs=outputs_video,
|
| 96 |
-
title="Pothole detector",
|
| 97 |
-
examples=video_path,
|
| 98 |
-
cache_examples=False,
|
| 99 |
-
)
|
| 100 |
|
| 101 |
-
gr.TabbedInterface(
|
| 102 |
-
[interface_image, interface_video],
|
| 103 |
-
tab_names=['Image inference', 'Video inference']
|
| 104 |
-
).queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
+
import io
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
FLUX1_APIKEY = os.getenv('FLUX1_APIKEY')
|
| 11 |
+
GEMINI_APIKEY = os.getenv('GEMINI_APIKEY')
|
| 12 |
+
|
| 13 |
+
print(FLUX1_APIKEY)
|
| 14 |
+
print(GEMINI_APIKEY)
|
| 15 |
+
|
| 16 |
+
def Main(prompt, inputText):
|
| 17 |
+
return GenerateImageFromText(prompt), GenerateTextLLM(inputText)
|
| 18 |
+
|
| 19 |
+
def GenerateImageFromText(prompt):
|
| 20 |
+
API_URL = "https://api-inference.huggingface.co/models/black-forest-labs/FLUX.1-dev"
|
| 21 |
+
headers = {"Authorization": f"Bearer {FLUX1_APIKEY}"}
|
| 22 |
+
|
| 23 |
+
def query(payload):
|
| 24 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 25 |
+
return response.content
|
| 26 |
+
|
| 27 |
+
image_bytes = query({
|
| 28 |
+
"inputs": prompt,
|
| 29 |
+
})
|
| 30 |
+
image = Image.open(io.BytesIO(image_bytes))
|
| 31 |
+
|
| 32 |
+
return image
|
| 33 |
+
|
| 34 |
+
def GenerateTextLLM(inputText):
|
| 35 |
+
|
| 36 |
+
url = f'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key={GEMINI_APIKEY}'
|
| 37 |
+
headers = {
|
| 38 |
+
'Content-Type': 'application/json'
|
| 39 |
+
}
|
| 40 |
+
data = {
|
| 41 |
+
"contents": [
|
| 42 |
+
{
|
| 43 |
+
"parts": [
|
| 44 |
+
{
|
| 45 |
+
"text": inputText
|
| 46 |
+
}
|
| 47 |
+
]
|
| 48 |
+
}
|
| 49 |
+
]
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
response = requests.post(url, headers=headers, json=data)
|
| 53 |
+
|
| 54 |
+
try:
|
| 55 |
+
return response.json()['candidates'][0]['content']['parts'][0]['text']
|
| 56 |
+
except:
|
| 57 |
+
return 'Error'
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
outputs_image = [
|
| 61 |
+
gr.components.Image(type="pil", label="Output Image"),
|
| 62 |
+
"text"
|
| 63 |
]
|
| 64 |
+
|
| 65 |
interface_image = gr.Interface(
|
| 66 |
+
fn=Main,
|
| 67 |
+
inputs=["text", "text"],
|
| 68 |
outputs=outputs_image,
|
| 69 |
+
title="Feur",
|
|
|
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
+
interface_image.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -44,4 +44,6 @@ thop>=0.1.1 # FLOPs computation
|
|
| 44 |
# roboflow
|
| 45 |
|
| 46 |
# HUB -----------------------------------------
|
| 47 |
-
GitPython>=3.1.24
|
|
|
|
|
|
|
|
|
| 44 |
# roboflow
|
| 45 |
|
| 46 |
# HUB -----------------------------------------
|
| 47 |
+
GitPython>=3.1.24
|
| 48 |
+
|
| 49 |
+
python-dotenv
|