Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,53 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as ui
|
| 3 |
-
|
| 4 |
|
| 5 |
-
#
|
| 6 |
API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 7 |
if API_KEY:
|
| 8 |
-
genai.
|
| 9 |
-
model = genai.GenerativeModel('gemini-pro')
|
| 10 |
else:
|
| 11 |
-
|
| 12 |
|
| 13 |
def gemini_response(user_message):
|
| 14 |
-
if not
|
| 15 |
return "Error: Gemini API Key is missing in Space Secrets!"
|
| 16 |
try:
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return response.text
|
| 19 |
except Exception as e:
|
| 20 |
return f"Error: {str(e)}"
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
def load_file(filename):
|
| 24 |
-
with open(filename, "r", encoding="utf-8") as f:
|
| 25 |
-
return f.read()
|
| 26 |
-
|
| 27 |
def get_full_ui():
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
# গ্রাডিও ইন্টারফেস
|
| 38 |
with ui.Blocks(title="Gemini AI") as demo:
|
| 39 |
-
# কাস্টম UI রেন্ডার
|
| 40 |
ui.HTML(get_full_ui())
|
| 41 |
|
| 42 |
-
# ব্যাকএন্ড
|
| 43 |
msg_input = ui.Textbox(visible=False)
|
| 44 |
msg_output = ui.Textbox(visible=False)
|
| 45 |
api_btn = ui.Button("Submit", visible=False)
|
| 46 |
|
| 47 |
-
# Gradio API এক্সপোজ করা (যা /call/predict দিয়ে সরাসরি কল হবে)
|
| 48 |
api_btn.click(fn=gemini_response, inputs=msg_input, outputs=msg_output, api_name="predict")
|
| 49 |
|
| 50 |
-
# এই লাইনটি এবার একদম ঠিক করে দেওয়া হলো
|
| 51 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as ui
|
| 3 |
+
from google import genai
|
| 4 |
|
| 5 |
+
# গুগলের নতুন GenAI ক্লায়েন্ট সেটআপ
|
| 6 |
API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 7 |
if API_KEY:
|
| 8 |
+
client = genai.Client(api_key=API_KEY)
|
|
|
|
| 9 |
else:
|
| 10 |
+
client = None
|
| 11 |
|
| 12 |
def gemini_response(user_message):
|
| 13 |
+
if not client:
|
| 14 |
return "Error: Gemini API Key is missing in Space Secrets!"
|
| 15 |
try:
|
| 16 |
+
# গুগলের নতুন রিকমেন্ডেড মডেল এবং মেথড
|
| 17 |
+
response = client.models.generate_content(
|
| 18 |
+
model='gemini-2.5-flash',
|
| 19 |
+
contents=user_message,
|
| 20 |
+
)
|
| 21 |
return response.text
|
| 22 |
except Exception as e:
|
| 23 |
return f"Error: {str(e)}"
|
| 24 |
|
| 25 |
+
# মেমোরি সেভ রেখে ফাইল লোড করার নিরাপদ ফাংশন
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def get_full_ui():
|
| 27 |
+
try:
|
| 28 |
+
with open("index.html", "r", encoding="utf-8") as f:
|
| 29 |
+
html = f.read()
|
| 30 |
+
with open("style.css", "r", encoding="utf-8") as f:
|
| 31 |
+
css = f.read()
|
| 32 |
+
with open("script.js", "r", encoding="utf-8") as f:
|
| 33 |
+
js = f.read()
|
| 34 |
+
|
| 35 |
+
# HTML এ CSS এবং JS ইনজেক্ট করা
|
| 36 |
+
full_code = html.replace("", f"<style>{css}</style>")
|
| 37 |
+
full_code = full_code.replace("", f"<script>{js}</script>")
|
| 38 |
+
return full_code
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"<h1>File Loading Error: {str(e)}</h1>"
|
| 41 |
|
| 42 |
+
# গ্রাডিও ইন্টারফেস
|
| 43 |
with ui.Blocks(title="Gemini AI") as demo:
|
|
|
|
| 44 |
ui.HTML(get_full_ui())
|
| 45 |
|
| 46 |
+
# ব্যাকএন্ড গেটওয়ে
|
| 47 |
msg_input = ui.Textbox(visible=False)
|
| 48 |
msg_output = ui.Textbox(visible=False)
|
| 49 |
api_btn = ui.Button("Submit", visible=False)
|
| 50 |
|
|
|
|
| 51 |
api_btn.click(fn=gemini_response, inputs=msg_input, outputs=msg_output, api_name="predict")
|
| 52 |
|
|
|
|
| 53 |
demo.launch()
|