Pepguy commited on
Commit
0998987
·
verified ·
1 Parent(s): e285d1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -12
app.py CHANGED
@@ -1,16 +1,88 @@
1
- from fastapi import FastAPI
2
- import subprocess
3
 
4
- app = FastAPI()
 
 
 
5
 
6
- @app.get("/")
7
- def greet_json():
8
- return {"Hello": "World!"}
9
 
10
- @app.get("/ffmpeg-version")
11
- def get_ffmpeg_version():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  try:
13
- result = subprocess.run(["ffmpeg", "-version"], capture_output=True, text=True)
14
- return {"ffmpeg_version": result.stdout.splitlines()[0]} # Return only the first line
15
- except FileNotFoundError:
16
- return {"error": "FFmpeg is not installed or not found in PATH"}
 
 
 
 
 
1
+ # To run this code you need to install the following dependencies:
2
+ # pip install flask google-genai
3
 
4
+ import os
5
+ from flask import Flask, request, render_template_string, jsonify
6
+ from google import genai
7
+ from google.genai import types
8
 
9
+ app = Flask(__name__)
 
 
10
 
11
+ HTML = """
12
+ <!DOCTYPE html>
13
+ <html lang="en">
14
+ <head>
15
+ <meta charset="UTF-8" />
16
+ <title>Gemini Test</title>
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>
25
+
26
+ <script>
27
+ const form = document.getElementById('genai-form');
28
+ form.addEventListener('submit', async e => {
29
+ e.preventDefault();
30
+ const prompt = document.getElementById('prompt').value;
31
+ const out = document.getElementById('output');
32
+ out.textContent = 'Generating…';
33
+ try {
34
+ const resp = await fetch('/generate', {
35
+ method: 'POST',
36
+ headers: { 'Content-Type': 'application/json' },
37
+ body: JSON.stringify({ prompt }),
38
+ });
39
+ const data = await resp.json();
40
+ out.textContent = data.result;
41
+ } catch (err) {
42
+ out.textContent = 'Error: ' + err;
43
+ }
44
+ });
45
+ </script>
46
+ </body>
47
+ </html>
48
+ """
49
+
50
+ def generate_from_gemini(prompt: str) -> str:
51
+ client = genai.Client("AIzaSyDolbPUZBPUPvQUu-RGktJmvnUpkcEKIYo")
52
+ model = "gemini-2.0-flash"
53
+ contents = [
54
+ types.Content(
55
+ role="user",
56
+ parts=[types.Part.from_text(text=prompt)],
57
+ )
58
+ ]
59
+ config = types.GenerateContentConfig(response_mime_type="text/plain")
60
+
61
+ result = []
62
+ for chunk in client.models.generate_content_stream(
63
+ model=model,
64
+ contents=contents,
65
+ config=config,
66
+ ):
67
+ result.append(chunk.text)
68
+ return "".join(result)
69
+
70
+ @app.route('/')
71
+ def index():
72
+ return render_template_string(HTML)
73
+
74
+ @app.route('/generate', methods=['POST'])
75
+ def gen():
76
+ data = request.get_json()
77
+ prompt = data.get("prompt", "")
78
+ if not prompt:
79
+ return jsonify({"error": "No prompt provided"}), 400
80
  try:
81
+ output = generate_from_gemini(prompt)
82
+ return jsonify({"result": output})
83
+ except Exception as e:
84
+ return jsonify({"error": str(e)}), 500
85
+
86
+ if __name__ == "__main__":
87
+ port = int(os.environ.get("PORT", 7860))
88
+ app.run(host="0.0.0.0", port=port)