Update app.py
Browse files
app.py
CHANGED
|
@@ -3,56 +3,72 @@ import gradio as gr
|
|
| 3 |
from google import genai
|
| 4 |
from google.genai import types
|
| 5 |
|
| 6 |
-
# Client
|
| 7 |
client_genai = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 8 |
|
| 9 |
-
def
|
| 10 |
"""
|
| 11 |
-
|
| 12 |
-
-
|
| 13 |
-
- Output: Ein String (oder Liste von Strings)
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
-
return f"
|
| 38 |
|
| 39 |
-
#
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
output_text = gr.Markdown(visible=False)
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 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()
|
|
|
|
| 3 |
from google import genai
|
| 4 |
from google.genai import types
|
| 5 |
|
| 6 |
+
# Client initialization
|
| 7 |
client_genai = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
| 8 |
|
| 9 |
+
def generate_with_history(input_text, history):
|
| 10 |
"""
|
| 11 |
+
- input_text: The new message from JS
|
| 12 |
+
- history: A list of previous messages [user, model, user, model...]
|
|
|
|
| 13 |
"""
|
| 14 |
+
if history is None:
|
| 15 |
+
history = []
|
| 16 |
+
|
| 17 |
+
# Build the contents list including history
|
| 18 |
+
# Gemini API expects a list of Content objects
|
| 19 |
+
contents = []
|
| 20 |
+
for msg in history:
|
| 21 |
+
contents.append(types.Content(
|
| 22 |
+
role=msg["role"],
|
| 23 |
+
parts=[types.Part.from_text(text=msg["content"])]
|
| 24 |
+
))
|
| 25 |
|
| 26 |
+
# Add the current user prompt
|
| 27 |
+
contents.append(types.Content(
|
| 28 |
+
role="user",
|
| 29 |
+
parts=[types.Part.from_text(text=input_text)]
|
| 30 |
+
))
|
| 31 |
+
|
| 32 |
tools = [types.Tool(google_search=types.GoogleSearch())]
|
| 33 |
config = types.GenerateContentConfig(tools=tools)
|
| 34 |
|
|
|
|
|
|
|
| 35 |
try:
|
|
|
|
|
|
|
| 36 |
response = client_genai.models.generate_content(
|
| 37 |
model="gemini-2.0-flash-001",
|
| 38 |
contents=contents,
|
| 39 |
config=config,
|
| 40 |
)
|
| 41 |
+
|
| 42 |
+
response_text = response.text
|
| 43 |
+
|
| 44 |
+
# Update history: add the current exchange
|
| 45 |
+
history.append({"role": "user", "content": input_text})
|
| 46 |
+
history.append({"role": "model", "content": response_text})
|
| 47 |
+
|
| 48 |
+
# Return the response for JS and the updated history for the State
|
| 49 |
+
return response_text, history
|
| 50 |
+
|
| 51 |
except Exception as e:
|
| 52 |
+
return f"Error: {str(e)}", history
|
| 53 |
|
| 54 |
+
# Building the Blocks interface
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
+
# State component to store the conversation across API calls
|
| 57 |
+
history_state = gr.State([])
|
|
|
|
| 58 |
|
| 59 |
+
# Hidden components to match your JS expectations
|
| 60 |
+
input_box = gr.Textbox(visible=False)
|
| 61 |
+
output_box = gr.Markdown(visible=False)
|
| 62 |
|
| 63 |
+
# The API endpoint
|
| 64 |
+
# Note: We include history_state in both inputs and outputs
|
| 65 |
+
btn = gr.Button("Submit", visible=False)
|
| 66 |
+
btn.click(
|
| 67 |
+
fn=generate_with_history,
|
| 68 |
+
inputs=[input_box, history_state],
|
| 69 |
+
outputs=[output_box, history_state],
|
| 70 |
api_name="generate"
|
| 71 |
)
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 74 |
demo.launch()
|