Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,22 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
|
|
|
| 4 |
|
| 5 |
username = os.environ.get("APP_USERNAME")
|
| 6 |
password = os.environ.get("APP_PASSWORD")
|
| 7 |
hf_token = os.environ.get("HF_TOKEN")
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
SYSTEM_PROMPT = "antworte kurz und knapp. optimiere deine antwort für sprachausgabe."
|
| 18 |
|
| 19 |
-
|
| 20 |
def build_message(input_text, history):
|
| 21 |
-
"""
|
| 22 |
-
flattened into a transcript that is sent along with the new input."""
|
| 23 |
if not history:
|
| 24 |
return input_text
|
| 25 |
|
|
@@ -37,24 +34,43 @@ def build_message(input_text, history):
|
|
| 37 |
|
| 38 |
def generate_with_history(input_text, history):
|
| 39 |
"""
|
| 40 |
-
- input_text:
|
| 41 |
-
- history:
|
| 42 |
"""
|
| 43 |
if history is None:
|
| 44 |
history = []
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
try:
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
temperature=0.7,
|
| 52 |
-
max_tokens=8096,
|
| 53 |
-
token_override="",
|
| 54 |
-
api_name="/chat",
|
| 55 |
)
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
|
| 58 |
history.append({"role": "user", "content": input_text})
|
| 59 |
history.append({"role": "model", "content": response_text})
|
| 60 |
|
|
@@ -64,7 +80,7 @@ def generate_with_history(input_text, history):
|
|
| 64 |
return f"Error: {str(e)}", history
|
| 65 |
|
| 66 |
|
| 67 |
-
#
|
| 68 |
with gr.Blocks() as demo:
|
| 69 |
history_state = gr.State([])
|
| 70 |
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from google import genai
|
| 4 |
+
from google.genai import types
|
| 5 |
|
| 6 |
username = os.environ.get("APP_USERNAME")
|
| 7 |
password = os.environ.get("APP_PASSWORD")
|
| 8 |
hf_token = os.environ.get("HF_TOKEN")
|
| 9 |
|
| 10 |
+
# Client initialization (v1beta is required for Google Search as a tool)
|
| 11 |
+
client_genai = genai.Client(
|
| 12 |
+
api_key=os.environ.get("GEMINI_API_KEY"),
|
| 13 |
+
http_options=types.HttpOptions(api_version="v1beta"),
|
| 14 |
)
|
| 15 |
|
|
|
|
|
|
|
| 16 |
SYSTEM_PROMPT = "antworte kurz und knapp. optimiere deine antwort für sprachausgabe."
|
| 17 |
|
|
|
|
| 18 |
def build_message(input_text, history):
|
| 19 |
+
"""Der Gesprächsverlauf wird zu einem Transkript zusammengefasst."""
|
|
|
|
| 20 |
if not history:
|
| 21 |
return input_text
|
| 22 |
|
|
|
|
| 34 |
|
| 35 |
def generate_with_history(input_text, history):
|
| 36 |
"""
|
| 37 |
+
- input_text: Die neue Nachricht
|
| 38 |
+
- history: Eine Liste von vorherigen Nachrichten [{'role': '...', 'content': '...'}, ...]
|
| 39 |
"""
|
| 40 |
if history is None:
|
| 41 |
history = []
|
| 42 |
|
| 43 |
+
contents = []
|
| 44 |
+
|
| 45 |
+
# Verlauf in das von der GenAI API erwartete Format konvertieren
|
| 46 |
+
for msg in history:
|
| 47 |
+
contents.append(types.Content(
|
| 48 |
+
role=msg["role"],
|
| 49 |
+
parts=[types.Part.from_text(text=msg["content"])]
|
| 50 |
+
))
|
| 51 |
+
|
| 52 |
+
# Neue Nachricht hinzufügen
|
| 53 |
+
contents.append(types.Content(
|
| 54 |
+
role="user",
|
| 55 |
+
parts=[types.Part.from_text(text=input_text)]
|
| 56 |
+
))
|
| 57 |
+
|
| 58 |
+
tools = [types.Tool(google_search=types.GoogleSearch())]
|
| 59 |
+
config = types.GenerateContentConfig(
|
| 60 |
+
tools=tools,
|
| 61 |
+
system_instruction=SYSTEM_PROMPT,
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
try:
|
| 65 |
+
response = client_genai.models.generate_content(
|
| 66 |
+
model="gemini-2.5-flash",
|
| 67 |
+
contents=contents,
|
| 68 |
+
config=config,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
)
|
| 70 |
|
| 71 |
+
response_text = response.text
|
| 72 |
+
|
| 73 |
+
# Verlauf aktualisieren
|
| 74 |
history.append({"role": "user", "content": input_text})
|
| 75 |
history.append({"role": "model", "content": response_text})
|
| 76 |
|
|
|
|
| 80 |
return f"Error: {str(e)}", history
|
| 81 |
|
| 82 |
|
| 83 |
+
# Gradio Blocks Interface
|
| 84 |
with gr.Blocks() as demo:
|
| 85 |
history_state = gr.State([])
|
| 86 |
|