Kvikontent commited on
Commit
fca1b98
β€’
1 Parent(s): 2f13692

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -20
app.py CHANGED
@@ -1,27 +1,62 @@
 
1
  import requests
2
- import base64
3
  import io
4
- from PIL import Image
5
- import gradio as gr
6
  import os
 
7
 
8
- API_KEY = os.environ["API_KEY"] # replace with your own API Token here
9
  API_URL = "https://api-inference.huggingface.co/models/Kvikontent/kviimager2.0"
10
- HEADERS = {'Authorization': f'Bearer {API_KEY}'}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
- def predictor(prompt):
13
- payload = {'inputs': prompt}
14
-
15
- try:
16
- response = requests.request("POST", url=API_URL, data=payload, headers=HEADERS)
17
-
18
- if not (response is None or response.status_code != 200):
19
- result = response.text[7:-5]
20
-
21
- return Image.open(io.BytesIO(base64.b64decode(result)))
22
-
23
- except Exception as e:
24
- print('Error processing request')
25
- raise ValueError(e)
26
 
27
- iface = gr.Interface(fn=predictor, inputs="textbox", outputs='image').launch()
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
  import requests
 
3
  import io
 
 
4
  import os
5
+ from PIL import Image
6
 
 
7
  API_URL = "https://api-inference.huggingface.co/models/Kvikontent/kviimager2.0"
8
+ api_key = os.environ.get('API_KEY')
9
+ headers = {"Authorization": f"Bearer {api_key}"}
10
+
11
+ def query(payload):
12
+ response = requests.post(API_URL, headers=headers, json=payload)
13
+ return response.content
14
+
15
+ def generate_image_from_prompt(prompt_text):
16
+ image_bytes = query({"inputs": prompt_text})
17
+ generated_image = Image.open(io.BytesIO(image_bytes))
18
+ return generated_image
19
+
20
+ title = "KVIImager 2.0 Demo 🎨"
21
+ description = "This app uses Hugging Face AI model to generate an image based on the provided text prompt πŸ–ΌοΈ."
22
+ examples = [
23
+ ["A peaceful garden with a small cottage 🏑"],
24
+ ["A colorful abstract painting with geometric shapes 🎨"],
25
+ ["A serene beach at sunset πŸŒ…"]
26
+ ]
27
+
28
+ css_styles = {
29
+ 'body': {
30
+ 'background-color': '#f4f4f4',
31
+ 'font-family': 'Arial, sans-serif'
32
+ },
33
+ 'title': {
34
+ 'color': 'navy',
35
+ 'font-size': '36px',
36
+ 'text-align': 'center',
37
+ 'margin-bottom': '20px'
38
+ },
39
+ 'textbox': {
40
+ 'border': '2px solid #008CBA',
41
+ 'border-radius': '5px',
42
+ 'padding': '10px',
43
+ 'margin-bottom': '20px',
44
+ 'width': '300px',
45
+ 'font-size': '16px'
46
+ },
47
+ 'output_image': {
48
+ 'box-shadow': '2px 2px 5px #888888'
49
+ }
50
+ }
51
 
52
+ input_prompt = gr.Textbox(label="Enter Prompt πŸ“", placeholder="E.g. 'A peaceful garden with a small cottage'")
53
+ output_generated_image = gr.Image(label="Generated Image")
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
+ gr.Interface(
56
+ generate_image_from_prompt,
57
+ inputs=input_prompt,
58
+ outputs=output_generated_image,
59
+ title=title,
60
+ description=description,
61
+ examples=examples
62
+ ).launch(inline=True, css=css_styles)