TraderXpatfx commited on
Commit
476a328
·
verified ·
1 Parent(s): be53199

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -93
app.py DELETED
@@ -1,93 +0,0 @@
1
- # app.py
2
- # HuggingFace Space में चलाने के लिए
3
- # pip install gradio google-genai
4
-
5
- import mimetypes
6
- import time
7
- import random
8
- import gradio as gr
9
- from google import genai
10
- from google.genai import types
11
- from google.api_core.exceptions import ResourceExhausted
12
-
13
- # ✅ API key
14
- API_KEY = "AIzaSyC5lJFAJdqxym1OErvYL07grkdhVfUrRBM"
15
- client = genai.Client(api_key=API_KEY)
16
-
17
-
18
- def generate_image(prompt, retries=3):
19
- model = "gemini-2.5-flash-image-preview"
20
-
21
- contents = [
22
- types.Content(
23
- role="user",
24
- parts=[types.Part.from_text(text=prompt)],
25
- ),
26
- ]
27
-
28
- generate_content_config = types.GenerateContentConfig(
29
- response_modalities=["IMAGE"],
30
- )
31
-
32
- file_index = 0
33
- images = []
34
-
35
- for attempt in range(retries):
36
- try:
37
- for chunk in client.models.generate_content_stream(
38
- model=model,
39
- contents=contents,
40
- config=generate_content_config,
41
- ):
42
- if (
43
- chunk.candidates
44
- and chunk.candidates[0].content
45
- and chunk.candidates[0].content.parts
46
- ):
47
- part = chunk.candidates[0].content.parts[0]
48
- if hasattr(part, "inline_data") and part.inline_data and part.inline_data.data:
49
- file_name = f"generated_{file_index}"
50
- file_index += 1
51
- inline_data = part.inline_data
52
- data_buffer = inline_data.data
53
- file_extension = mimetypes.guess_extension(inline_data.mime_type) or ".png"
54
- file_path = f"{file_name}{file_extension}"
55
-
56
- # Save image
57
- with open(file_path, "wb") as f:
58
- f.write(data_buffer)
59
-
60
- images.append(file_path)
61
-
62
- return images if images else None
63
-
64
- except ResourceExhausted as e:
65
- # ⚠️ अगर quota exceed हुआ
66
- error_msg = str(e)
67
- if "RESOURCE_EXHAUSTED" in error_msg:
68
- if "FreeTier" in error_msg:
69
- return [f"❌ Free quota खत्म हो गया है! कृपया 24 घंटे बाद फिर से कोशिश करें या Google Cloud Billing enable करें."]
70
- else:
71
- wait_time = 30 + random.randint(0, 20)
72
- print(f"Quota hit, retrying in {wait_time} sec...")
73
- time.sleep(wait_time)
74
- else:
75
- return [f"⚠️ Error: {error_msg}"]
76
-
77
- except Exception as e:
78
- return [f"⚠️ Unknown Error: {str(e)}"]
79
-
80
- return [f"❌ Retry limit खत्म हो गई, बाद में कोशिश करें."]
81
-
82
-
83
- # ✅ Gradio UI (बिना .style)
84
- demo = gr.Interface(
85
- fn=generate_image,
86
- inputs=gr.Textbox(label="Enter your prompt", placeholder="Describe the image you want..."),
87
- outputs=gr.Gallery(label="Generated Images", show_label=True),
88
- title="Gemini Image Generator",
89
- description="Enter a text prompt and generate AI-powered images using Google Gemini."
90
- )
91
-
92
- if __name__ == "__main__":
93
- demo.launch()