Update app.py
Browse files
app.py
CHANGED
|
@@ -4,40 +4,55 @@ from google import genai
|
|
| 4 |
from google.genai import types
|
| 5 |
|
| 6 |
# Client initialisieren
|
| 7 |
-
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
contents = [
|
| 12 |
types.Content(
|
| 13 |
role="user",
|
| 14 |
-
parts=[types.Part.from_text(text=
|
| 15 |
),
|
| 16 |
]
|
| 17 |
|
| 18 |
-
# Konfiguration mit Google Search Tool
|
| 19 |
tools = [types.Tool(google_search=types.GoogleSearch())]
|
| 20 |
config = types.GenerateContentConfig(tools=tools)
|
| 21 |
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
# Gradio Interface
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 43 |
demo.launch()
|
|
|
|
| 4 |
from google.genai import types
|
| 5 |
|
| 6 |
# Client initialisieren
|
| 7 |
+
client_genai = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 8 |
|
| 9 |
+
def generate_for_js(input_text):
|
| 10 |
+
"""
|
| 11 |
+
Diese Funktion entspricht exakt der Erwartung des JS-Clients:
|
| 12 |
+
- Input: input_text
|
| 13 |
+
- Output: Ein String (oder Liste von Strings)
|
| 14 |
+
"""
|
| 15 |
contents = [
|
| 16 |
types.Content(
|
| 17 |
role="user",
|
| 18 |
+
parts=[types.Part.from_text(text=input_text)],
|
| 19 |
),
|
| 20 |
]
|
| 21 |
|
|
|
|
| 22 |
tools = [types.Tool(google_search=types.GoogleSearch())]
|
| 23 |
config = types.GenerateContentConfig(tools=tools)
|
| 24 |
|
| 25 |
+
full_response = ""
|
| 26 |
|
| 27 |
+
try:
|
| 28 |
+
# Da das JS await client.predict nutzt, sammeln wir hier den gesamten Text
|
| 29 |
+
# Falls du echtes Streaming im JS willst, müsste dort 'client.submit' genutzt werden.
|
| 30 |
+
response = client_genai.models.generate_content(
|
| 31 |
+
model="gemini-2.0-flash-001",
|
| 32 |
+
contents=contents,
|
| 33 |
+
config=config,
|
| 34 |
+
)
|
| 35 |
+
return response.text
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Fehler: {str(e)}"
|
| 38 |
|
| 39 |
+
# Gradio Interface Definition
|
| 40 |
+
with gr.Blocks() as demo:
|
| 41 |
+
# Der API-Name muss "generate" sein, damit /generate im JS funktioniert
|
| 42 |
+
prompt_input = gr.Textbox(label="Input Text", visible=False)
|
| 43 |
+
output_text = gr.Markdown(visible=False)
|
| 44 |
+
|
| 45 |
+
submit_btn = gr.Button("Senden", visible=False)
|
| 46 |
+
|
| 47 |
+
# Hier definieren wir den Endpunkt explizit
|
| 48 |
+
submit_btn.click(
|
| 49 |
+
fn=generate_for_js,
|
| 50 |
+
inputs=prompt_input,
|
| 51 |
+
outputs=output_text,
|
| 52 |
+
api_name="generate"
|
| 53 |
+
)
|
| 54 |
|
| 55 |
if __name__ == "__main__":
|
| 56 |
+
# Wichtig: Wenn du es auf Hugging Face Spaces hostest (mgokg/webapi),
|
| 57 |
+
# wird der api_name automatisch unter /predict/generate verfügbar.
|
| 58 |
demo.launch()
|