dolphinium commited on
Commit
1c47120
1 Parent(s): b03e17c

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. .github/workflows/update_space.yml +28 -0
  2. README.md +3 -9
  3. app.py +112 -0
  4. requirements.txt +3 -0
.github/workflows/update_space.yml ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Run Python script
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ build:
10
+ runs-on: ubuntu-latest
11
+
12
+ steps:
13
+ - name: Checkout
14
+ uses: actions/checkout@v2
15
+
16
+ - name: Set up Python
17
+ uses: actions/setup-python@v2
18
+ with:
19
+ python-version: '3.9'
20
+
21
+ - name: Install Gradio
22
+ run: python -m pip install gradio
23
+
24
+ - name: Log in to Hugging Face
25
+ run: python -c 'import huggingface_hub; huggingface_hub.login(token="${{ secrets.hf_token }}")'
26
+
27
+ - name: Deploy to Spaces
28
+ run: gradio deploy
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Image Gen Tr
3
- emoji: ⚡
4
- colorFrom: gray
5
- colorTo: indigo
6
- sdk: gradio
7
- sdk_version: 4.43.0
8
  app_file: app.py
9
- pinned: false
 
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: image_gen_tr
 
 
 
 
 
3
  app_file: app.py
4
+ sdk: gradio
5
+ sdk_version: 4.41.0
6
  ---
 
 
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import io
4
+ import requests, json
5
+ from PIL import Image
6
+ import base64
7
+ from dotenv import load_dotenv, find_dotenv
8
+
9
+ _ = load_dotenv(find_dotenv()) # read local .env file
10
+ hf_api_key = os.environ['HF_API_KEY']
11
+
12
+ # Load the translation model (Turkish to English)
13
+ API_URL = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-tr-en"
14
+
15
+ headers = {
16
+ "Authorization": f"Bearer {hf_api_key}",
17
+ "Content-Type": "application/json"
18
+ }
19
+
20
+ # Text-to-image endpoint
21
+ def get_completion(inputs, parameters=None, ENDPOINT_URL=os.environ['HF_API_TTI_STABILITY_AI']):
22
+ data = {"inputs": inputs}
23
+ if parameters is not None:
24
+ data.update({"parameters": parameters})
25
+
26
+ response = requests.post(ENDPOINT_URL, headers=headers, data=json.dumps(data))
27
+
28
+ # Check the content type of the response
29
+ content_type = response.headers.get('Content-Type', '')
30
+ print(content_type)
31
+ if 'application/json' in content_type:
32
+ return json.loads(response.content.decode("utf-8"))
33
+ elif 'image/' in content_type:
34
+ return response.content # return raw image data
35
+
36
+ response.raise_for_status() # raise an error for unexpected content types
37
+
38
+ # A helper function to convert the PIL image to base64
39
+ def base64_to_pil(img_base64):
40
+ base64_decoded = base64.b64decode(img_base64)
41
+ byte_stream = io.BytesIO(base64_decoded)
42
+ pil_image = Image.open(byte_stream)
43
+ return pil_image
44
+
45
+ def query(payload):
46
+ response = requests.post(API_URL, headers=headers, json=payload)
47
+ return response.json()
48
+
49
+ # Translation function
50
+ def translate_to_english(text):
51
+ try:
52
+ # Translate the input from Turkish to English
53
+ translation = query({"inputs": text})
54
+ print(translation)
55
+ translated_text = translation[0]['translation_text']
56
+ return translated_text
57
+ except Exception as e:
58
+ print(f"Translation error: {e}")
59
+ return text # If translation fails, return original text
60
+
61
+ # Main generation function with translation
62
+ def generate(prompt, negative_prompt, steps, guidance, width, height):
63
+ # Translate the prompt to English if it's in Turkish
64
+ translated_prompt = translate_to_english(prompt)
65
+ print(f"Translated Prompt: {translated_prompt}")
66
+
67
+ params = {
68
+ "negative_prompt": negative_prompt,
69
+ "num_inference_steps": steps,
70
+ "guidance_scale": guidance,
71
+ "width": width,
72
+ "height": height
73
+ }
74
+
75
+ output = get_completion(translated_prompt, params)
76
+
77
+ # Check if the output is an image (bytes) or JSON (dict)
78
+ if isinstance(output, dict):
79
+ raise ValueError("Expected an image but received JSON: {}".format(output))
80
+
81
+ # If output is raw image data, convert it to a PIL image
82
+ result_image = Image.open(io.BytesIO(output))
83
+ return (translated_prompt, result_image)
84
+
85
+ with gr.Blocks() as demo:
86
+ gr.Markdown("## Image Generation with Turkish Inputs")
87
+ gr.Markdown("### [`stabilityai/stable-diffusion-xl-base-1.0`](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0) and [`Helsinki-NLP/opus-mt-tr-en`](https://huggingface.co/Helsinki-NLP/opus-mt-tr-en) models work under the hood!")
88
+
89
+ with gr.Row():
90
+ with gr.Column(scale=4):
91
+ prompt = gr.Textbox(label="Your prompt (in Turkish or English)") # Accept Turkish or English input
92
+ with gr.Column(scale=1, min_width=50):
93
+ btn = gr.Button("Submit")
94
+
95
+ with gr.Accordion("Advanced options", open=False):
96
+ negative_prompt = gr.Textbox(label="Negative prompt")
97
+ with gr.Row():
98
+ with gr.Column():
99
+ steps = gr.Slider(label="Inference Steps", minimum=1, maximum=100, value=50,
100
+ info="In how many steps will the denoiser denoise the image?")
101
+ guidance = gr.Slider(label="Guidance Scale", minimum=1, maximum=20, value=7,
102
+ info="Controls how much the text prompt influences the result")
103
+ with gr.Column():
104
+ width = gr.Slider(label="Width", minimum=64, maximum=1024, step=64, value=512)
105
+ height = gr.Slider(label="Height", minimum=64, maximum=1024, step=64, value=512)
106
+ translated_text = gr.Textbox(label="Translated text")
107
+ output = gr.Image(label="Result")
108
+
109
+ btn.click(fn=generate, inputs=[prompt, negative_prompt, steps, guidance, width, height], outputs=[translated_text, output])
110
+
111
+ gr.close_all()
112
+ demo.launch(share=True)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ gradio
2
+ pillow
3
+ python-dotenv