Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,30 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
|
| 5 |
# Client initialization
|
| 6 |
-
client =
|
| 7 |
-
api_key=os.environ.get("GROQ_API_KEY"),
|
| 8 |
-
)
|
| 9 |
|
| 10 |
SYSTEM_PROMPT = "antworte kurz und knapp. optimiere deine antwort für sprachausgabe."
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def generate_with_history(input_text, history):
|
| 13 |
"""
|
| 14 |
- input_text: The new message from JS
|
|
@@ -17,41 +33,17 @@ def generate_with_history(input_text, history):
|
|
| 17 |
if history is None:
|
| 18 |
history = []
|
| 19 |
|
| 20 |
-
# Build the messages list including history
|
| 21 |
-
# Note: Groq uses "assistant" instead of Gemini's "model"
|
| 22 |
-
messages = [
|
| 23 |
-
{"role": "system", "content": SYSTEM_PROMPT}
|
| 24 |
-
]
|
| 25 |
-
|
| 26 |
-
for msg in history:
|
| 27 |
-
role = "assistant" if msg["role"] == "model" else msg["role"]
|
| 28 |
-
messages.append({
|
| 29 |
-
"role": role,
|
| 30 |
-
"content": msg["content"]
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
# Add the current user prompt
|
| 34 |
-
messages.append({
|
| 35 |
-
"role": "user",
|
| 36 |
-
"content": input_text
|
| 37 |
-
})
|
| 38 |
-
|
| 39 |
try:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
temperature=0.7,
|
| 44 |
-
max_tokens=
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
{
|
| 48 |
-
"type": "browser_search"
|
| 49 |
-
}
|
| 50 |
-
]
|
| 51 |
)
|
| 52 |
|
| 53 |
-
response_text = response.choices[0].message.content
|
| 54 |
-
|
| 55 |
# Update history: add the current exchange
|
| 56 |
history.append({"role": "user", "content": input_text})
|
| 57 |
history.append({"role": "model", "content": response_text})
|
|
@@ -61,6 +53,7 @@ def generate_with_history(input_text, history):
|
|
| 61 |
except Exception as e:
|
| 62 |
return f"Error: {str(e)}", history
|
| 63 |
|
|
|
|
| 64 |
# Building the Blocks interface
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
history_state = gr.State([])
|
|
@@ -74,7 +67,7 @@ with gr.Blocks() as demo:
|
|
| 74 |
fn=generate_with_history,
|
| 75 |
inputs=[input_box, history_state],
|
| 76 |
outputs=[output_box, history_state],
|
| 77 |
-
api_name="generate"
|
| 78 |
)
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from gradio_client import Client
|
| 3 |
|
| 4 |
# Client initialization
|
| 5 |
+
client = Client("mgokg/puter")
|
|
|
|
|
|
|
| 6 |
|
| 7 |
SYSTEM_PROMPT = "antworte kurz und knapp. optimiere deine antwort für sprachausgabe."
|
| 8 |
|
| 9 |
+
|
| 10 |
+
def build_message(input_text, history):
|
| 11 |
+
"""The puter API takes a single message string, so the history is
|
| 12 |
+
flattened into a transcript that is sent along with the new input."""
|
| 13 |
+
if not history:
|
| 14 |
+
return input_text
|
| 15 |
+
|
| 16 |
+
lines = []
|
| 17 |
+
for msg in history:
|
| 18 |
+
speaker = "Assistent" if msg["role"] in ("model", "assistant") else "Nutzer"
|
| 19 |
+
lines.append(f"{speaker}: {msg['content']}")
|
| 20 |
+
|
| 21 |
+
transcript = "\n".join(lines)
|
| 22 |
+
return (
|
| 23 |
+
f"Bisheriger Gesprächsverlauf:\n{transcript}\n\n"
|
| 24 |
+
f"Neue Nachricht des Nutzers:\n{input_text}"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
def generate_with_history(input_text, history):
|
| 29 |
"""
|
| 30 |
- input_text: The new message from JS
|
|
|
|
| 33 |
if history is None:
|
| 34 |
history = []
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
+
response_text = client.predict(
|
| 38 |
+
message=build_message(input_text, history),
|
| 39 |
+
model="gpt-4o-mini",
|
| 40 |
+
system_prompt=SYSTEM_PROMPT,
|
| 41 |
temperature=0.7,
|
| 42 |
+
max_tokens=8096,
|
| 43 |
+
token_override="",
|
| 44 |
+
api_name="/chat",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
|
|
|
|
|
|
|
| 47 |
# Update history: add the current exchange
|
| 48 |
history.append({"role": "user", "content": input_text})
|
| 49 |
history.append({"role": "model", "content": response_text})
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
return f"Error: {str(e)}", history
|
| 55 |
|
| 56 |
+
|
| 57 |
# Building the Blocks interface
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
history_state = gr.State([])
|
|
|
|
| 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__":
|