Spaces:
Running

mgokg commited on
Commit
66b9a6b
·
verified ·
1 Parent(s): 488b248

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -22
app.py CHANGED
@@ -1,25 +1,22 @@
1
  import os
2
  import gradio as gr
3
- from gradio_client import Client
 
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
- client = Client(
10
- "mgokg/puter",
11
- token=hf_token, # nur nötig, falls der Space privat ist
12
- auth=(username, password), # für launch(auth=...) im Ziel-Space
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
- """The puter API takes a single message string, so the history is
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: The new message from JS
41
- - history: A list of previous messages [user, model, user, model...]
42
  """
43
  if history is None:
44
  history = []
45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  try:
47
- response_text = client.predict(
48
- message=build_message(input_text, history),
49
- model="gpt-4o-mini",
50
- system_prompt=SYSTEM_PROMPT,
51
- temperature=0.7,
52
- max_tokens=8096,
53
- token_override="",
54
- api_name="/chat",
55
  )
56
 
57
- # Update history: add the current exchange
 
 
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
- # Building the Blocks interface
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