Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# To run this code you need to install
|
| 2 |
# pip install flask google-genai
|
| 3 |
|
| 4 |
import os
|
|
@@ -17,8 +17,9 @@ HTML = """
|
|
| 17 |
</head>
|
| 18 |
<body style="font-family:sans-serif;padding:2rem;">
|
| 19 |
<h1>Gemini-2.0-Flash Test</h1>
|
| 20 |
-
<form id="genai-form">
|
| 21 |
-
<textarea id="prompt" rows="6" cols="60" placeholder="Enter your prompt here"></textarea><br/><br/>
|
|
|
|
| 22 |
<button type="submit">Generate</button>
|
| 23 |
</form>
|
| 24 |
<pre id="output" style="background:#f4f4f4;padding:1rem;margin-top:1rem;"></pre>
|
|
@@ -27,18 +28,17 @@ HTML = """
|
|
| 27 |
const form = document.getElementById('genai-form');
|
| 28 |
form.addEventListener('submit', async e => {
|
| 29 |
e.preventDefault();
|
| 30 |
-
const prompt = document.getElementById('prompt').value.trim();
|
| 31 |
const out = document.getElementById('output');
|
| 32 |
out.textContent = 'Generating…';
|
| 33 |
|
|
|
|
|
|
|
| 34 |
try {
|
| 35 |
const resp = await fetch('/generate', {
|
| 36 |
method: 'POST',
|
| 37 |
-
|
| 38 |
-
body: JSON.stringify({ text: prompt }),
|
| 39 |
});
|
| 40 |
|
| 41 |
-
// if you want to debug status codes:
|
| 42 |
if (!resp.ok) {
|
| 43 |
const errText = await resp.text();
|
| 44 |
throw new Error(`Server returned ${resp.status}: ${errText}`);
|
|
@@ -46,7 +46,6 @@ HTML = """
|
|
| 46 |
|
| 47 |
const data = await resp.json();
|
| 48 |
if (data.error) {
|
| 49 |
-
// show the backend error message
|
| 50 |
out.textContent = 'Error: ' + data.error;
|
| 51 |
} else if (data.result) {
|
| 52 |
out.textContent = data.result;
|
|
@@ -63,27 +62,28 @@ HTML = """
|
|
| 63 |
</html>
|
| 64 |
"""
|
| 65 |
|
| 66 |
-
def generate_from_gemini(prompt: str) -> str:
|
| 67 |
client = genai.Client(
|
| 68 |
-
api_key="AIzaSyDolbPUZBPUPvQUu-RGktJmvnUpkcEKIYo",
|
| 69 |
)
|
| 70 |
|
| 71 |
model = "gemini-2.0-flash"
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
| 78 |
config = types.GenerateContentConfig(response_mime_type="text/plain")
|
| 79 |
|
| 80 |
-
# Synchronous, non-streaming call:
|
| 81 |
response = client.models.generate_content(
|
| 82 |
model=model,
|
| 83 |
contents=contents,
|
| 84 |
config=config,
|
| 85 |
)
|
| 86 |
-
# The full text is in response.text
|
| 87 |
return response.text
|
| 88 |
|
| 89 |
@app.route('/')
|
|
@@ -92,14 +92,20 @@ def index():
|
|
| 92 |
|
| 93 |
@app.route('/generate', methods=['POST'])
|
| 94 |
def gen():
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
try:
|
| 101 |
-
output = generate_from_gemini(prompt)
|
| 102 |
-
app.logger.info("Generation succeeded, length=%d", len(output))
|
| 103 |
return jsonify({"result": output})
|
| 104 |
except Exception as e:
|
| 105 |
app.logger.exception("Generation failed")
|
|
|
|
| 1 |
+
# To run this code you need to install:
|
| 2 |
# pip install flask google-genai
|
| 3 |
|
| 4 |
import os
|
|
|
|
| 17 |
</head>
|
| 18 |
<body style="font-family:sans-serif;padding:2rem;">
|
| 19 |
<h1>Gemini-2.0-Flash Test</h1>
|
| 20 |
+
<form id="genai-form" enctype="multipart/form-data">
|
| 21 |
+
<textarea id="prompt" name="text" rows="6" cols="60" placeholder="Enter your prompt here"></textarea><br/><br/>
|
| 22 |
+
<input type="file" id="image" name="image" accept="image/*" /><br/><br/>
|
| 23 |
<button type="submit">Generate</button>
|
| 24 |
</form>
|
| 25 |
<pre id="output" style="background:#f4f4f4;padding:1rem;margin-top:1rem;"></pre>
|
|
|
|
| 28 |
const form = document.getElementById('genai-form');
|
| 29 |
form.addEventListener('submit', async e => {
|
| 30 |
e.preventDefault();
|
|
|
|
| 31 |
const out = document.getElementById('output');
|
| 32 |
out.textContent = 'Generating…';
|
| 33 |
|
| 34 |
+
const formData = new FormData(form);
|
| 35 |
+
|
| 36 |
try {
|
| 37 |
const resp = await fetch('/generate', {
|
| 38 |
method: 'POST',
|
| 39 |
+
body: formData
|
|
|
|
| 40 |
});
|
| 41 |
|
|
|
|
| 42 |
if (!resp.ok) {
|
| 43 |
const errText = await resp.text();
|
| 44 |
throw new Error(`Server returned ${resp.status}: ${errText}`);
|
|
|
|
| 46 |
|
| 47 |
const data = await resp.json();
|
| 48 |
if (data.error) {
|
|
|
|
| 49 |
out.textContent = 'Error: ' + data.error;
|
| 50 |
} else if (data.result) {
|
| 51 |
out.textContent = data.result;
|
|
|
|
| 62 |
</html>
|
| 63 |
"""
|
| 64 |
|
| 65 |
+
def generate_from_gemini(prompt: str, image_bytes=None, mime_type=None) -> str:
|
| 66 |
client = genai.Client(
|
| 67 |
+
api_key="AIzaSyDolbPUZBPUPvQUu-RGktJmvnUpkcEKIYo", # replace with env var for safety
|
| 68 |
)
|
| 69 |
|
| 70 |
model = "gemini-2.0-flash"
|
| 71 |
+
parts = []
|
| 72 |
+
|
| 73 |
+
if prompt:
|
| 74 |
+
parts.append(types.Part.from_text(prompt))
|
| 75 |
+
|
| 76 |
+
if image_bytes:
|
| 77 |
+
parts.append(types.Part.from_bytes(data=image_bytes, mime_type=mime_type or "image/png"))
|
| 78 |
+
|
| 79 |
+
contents = [types.Content(role="user", parts=parts)]
|
| 80 |
config = types.GenerateContentConfig(response_mime_type="text/plain")
|
| 81 |
|
|
|
|
| 82 |
response = client.models.generate_content(
|
| 83 |
model=model,
|
| 84 |
contents=contents,
|
| 85 |
config=config,
|
| 86 |
)
|
|
|
|
| 87 |
return response.text
|
| 88 |
|
| 89 |
@app.route('/')
|
|
|
|
| 92 |
|
| 93 |
@app.route('/generate', methods=['POST'])
|
| 94 |
def gen():
|
| 95 |
+
prompt = request.form.get("text", "")
|
| 96 |
+
file = request.files.get("image")
|
| 97 |
+
|
| 98 |
+
image_bytes = None
|
| 99 |
+
mime_type = None
|
| 100 |
+
if file:
|
| 101 |
+
image_bytes = file.read()
|
| 102 |
+
mime_type = file.mimetype
|
| 103 |
+
|
| 104 |
+
if not prompt and not image_bytes:
|
| 105 |
+
return jsonify({"error": "No input provided"}), 400
|
| 106 |
+
|
| 107 |
try:
|
| 108 |
+
output = generate_from_gemini(prompt, image_bytes, mime_type)
|
|
|
|
| 109 |
return jsonify({"result": output})
|
| 110 |
except Exception as e:
|
| 111 |
app.logger.exception("Generation failed")
|