Spaces:
Runtime error
Runtime error
Bipin Krishnan
commited on
Commit
·
b53b5ae
1
Parent(s):
5d2639d
added files
Browse files- app.py +75 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
|
4 |
+
from glob import glob
|
5 |
+
from PIL import Image
|
6 |
+
import os
|
7 |
+
from icrawler.builtin import GoogleImageCrawler
|
8 |
+
|
9 |
+
def download_image(query, out_file):
|
10 |
+
google_crawler = GoogleImageCrawler(storage={'root_dir': './'})
|
11 |
+
google_crawler.crawl(keyword=query, max_num=1, overwrite=True)
|
12 |
+
os.rename(glob("./000001.*")[0], out_file)
|
13 |
+
|
14 |
+
def generate_story(prompt):
|
15 |
+
story = storygen(f"{prompt}")[0]['generated_text']
|
16 |
+
return story
|
17 |
+
|
18 |
+
def start_neural_style_transfer(img1_name, img2_name):
|
19 |
+
img1_filename, img2_filename = "style_transfer_1.jpg", "style_transfer_2.jpg"
|
20 |
+
|
21 |
+
download_image(img1_name, img1_filename)
|
22 |
+
download_image(img2_name, img2_filename)
|
23 |
+
|
24 |
+
styled_image = nst(img1_filename, img2_filename)
|
25 |
+
|
26 |
+
pil_img = Image.open(styled_image)
|
27 |
+
|
28 |
+
return pil_img
|
29 |
+
|
30 |
+
def detect_objects(file_name):
|
31 |
+
out_img = detectron(file_name)
|
32 |
+
pil_img = Image.open(out_img)
|
33 |
+
return pil_img
|
34 |
+
|
35 |
+
def main(text_input):
|
36 |
+
text_output, image_output, metadata = None, None, None
|
37 |
+
|
38 |
+
task_type_q = f"User: {text_input}\nWhat is the task the user is asking to do?\n \
|
39 |
+
- story generation task\n \
|
40 |
+
- image style transfer task\n \
|
41 |
+
- object detection task"
|
42 |
+
task = t0pp(task_type_q)
|
43 |
+
task = task.lower().replace('.', '')
|
44 |
+
|
45 |
+
if task=="story generation task":
|
46 |
+
story_prompt = t0pp(f"User: {text_input}\nWhat story is the user asking for?")
|
47 |
+
text_output = generate_story(story_prompt)
|
48 |
+
metadata = f"Prompt used to generate the story:\n{story_prompt}"
|
49 |
+
|
50 |
+
elif task=="image style transfer task":
|
51 |
+
img1_name = t0pp(f"User: {text_input}\nWhat is the name of the picture to which style is to be tranferred?")
|
52 |
+
img2_name = t0pp(f"User: {text_input}\nWhat is the name of the picture from which style is to be tranferred?")
|
53 |
+
image_output = start_neural_style_transfer(img1_name, img2_name)
|
54 |
+
metadata = f"Image from which style is to be transferred: {img2_name}\nImage to which style is to be transferred: {img1_name}"
|
55 |
+
|
56 |
+
elif task=="object detection task":
|
57 |
+
img_file = "object_detection.jpg"
|
58 |
+
img_name = t0pp(f"User: {text_input}\nWhat image is the user referring to?")
|
59 |
+
download_image(img_name, img_file)
|
60 |
+
image_output = detect_objects(img_file)
|
61 |
+
metadata = f"Image from which objects are to be detected: {img_name}"
|
62 |
+
|
63 |
+
return text_output, image_output
|
64 |
+
|
65 |
+
if __name__=="__main__":
|
66 |
+
t0pp = gr.Interface.load("huggingface/bigscience/T0pp")
|
67 |
+
storygen = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
68 |
+
nst = gr.Interface.load("spaces/luca-martial/neural-style-transfer")
|
69 |
+
detectron = gr.Interface.load("spaces/akhaliq/Detectron2")
|
70 |
+
|
71 |
+
gr.Interface(
|
72 |
+
main,
|
73 |
+
inputs=gr.inputs.Textbox(lines=5, label="Input"),
|
74 |
+
outputs=[gr.outputs.Textbox(label="Output"), gr.outputs.Image(label="Ouptut"),]
|
75 |
+
).launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
icrawler
|