rt2195355 commited on
Commit
d57b422
·
1 Parent(s): e467d55

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import os
4
+ import io
5
+ import IPython.display
6
+ from PIL import Image
7
+ import base64
8
+
9
+ hf_api_key = "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"
10
+ import requests, json
11
+
12
+ def get_completion(inputs, parameters=None, ENDPOINT_URL="https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"):
13
+ headers = {
14
+ "Authorization": f"Bearer {hf_api_key}",
15
+ "Content-Type": "application/json"
16
+ }
17
+ data = { "inputs": inputs }
18
+ if parameters is not None:
19
+ data.update({"parameters": parameters})
20
+ response = requests.request("POST",
21
+ ENDPOINT_URL,
22
+ headers=headers,
23
+ data=json.dumps(data))
24
+ return response.content
25
+
26
+ import gradio as gr
27
+
28
+ # A helper function to convert the PIL image to base64
29
+ # so you can send it to the API
30
+ def base64_to_pil(img_base64):
31
+ base64_decoded = base64.b64decode(img_base64)
32
+ byte_stream = io.BytesIO(base64_decoded)
33
+ pil_image = Image.open(byte_stream)
34
+ return pil_image
35
+
36
+ def generate(prompt):
37
+ output = get_completion(prompt)
38
+ result_image = Image.open(io.BytesIO(output))
39
+ return result_image
40
+
41
+ gr.close_all()
42
+ demo = gr.Interface(
43
+ fn=generate,
44
+ inputs=[gr.Textbox(label="Your prompt")],
45
+ outputs=[gr.Image(label="Result")],
46
+ title="Image Generation with Stable Diffusion",
47
+ description="Generate any image with Stable Diffusion",
48
+ allow_flagging="never",
49
+ )
50
+
51
+ demo.launch(inline = False)