Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import requests
|
|
|
|
| 4 |
|
| 5 |
SYSTEM_PROMPT = "Als LLM besteht deine Aufgabe darin, detaillierte Aufforderungen zu generieren, die mit 'Generiere das Bild' beginnen, für Bildgenerierungsmodelle basierend auf Benutzereingaben. Sei beschreibend und spezifisch, aber achte auch darauf, dass deine Aufforderungen klar und prägnant sind."
|
| 6 |
TITLE = "Bildanreger"
|
|
@@ -15,11 +16,11 @@ def build_input_prompt(message, chatbot, system_prompt):
|
|
| 15 |
"""
|
| 16 |
Konstruiert die Eingabeaufforderung aus den Chatbot-Interaktionen und der aktuellen Nachricht.
|
| 17 |
"""
|
| 18 |
-
input_prompt = "\n" + system_prompt + "
|
| 19 |
for interaction in chatbot:
|
| 20 |
-
input_prompt
|
| 21 |
|
| 22 |
-
input_prompt
|
| 23 |
return input_prompt
|
| 24 |
|
| 25 |
def post_request_beta(payload):
|
|
@@ -30,7 +31,7 @@ def post_request_beta(payload):
|
|
| 30 |
response.raise_for_status() # Löst einen HTTPError aus, wenn die HTTP-Anfrage einen erfolglosen Statuscode zurückgegeben hat
|
| 31 |
return response.json()
|
| 32 |
|
| 33 |
-
def predict_beta(message, chatbot=[], system_prompt=
|
| 34 |
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
| 35 |
data = {
|
| 36 |
"inputs": input_prompt
|
|
@@ -41,36 +42,34 @@ def predict_beta(message, chatbot=[], system_prompt=""):
|
|
| 41 |
json_obj = response_data[0]
|
| 42 |
|
| 43 |
if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
|
| 44 |
-
|
| 45 |
-
return bot_message
|
| 46 |
elif 'error' in json_obj:
|
| 47 |
-
raise
|
| 48 |
else:
|
| 49 |
-
|
| 50 |
-
raise gr.Error(warning_msg)
|
| 51 |
except requests.HTTPError as e:
|
| 52 |
-
|
| 53 |
-
raise gr.Error(error_msg)
|
| 54 |
except json.JSONDecodeError as e:
|
| 55 |
-
|
| 56 |
-
raise gr.Error(error_msg)
|
| 57 |
|
| 58 |
-
def test_preview_chatbot(message, history):
|
| 59 |
-
response = predict_beta(message, history
|
| 60 |
-
|
| 61 |
-
response
|
| 62 |
-
return response
|
| 63 |
|
| 64 |
with gr.Blocks(theme='syddharth/gray-minimal') as app:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
app.add(demo)
|
| 75 |
|
| 76 |
app.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
+
import json
|
| 5 |
|
| 6 |
SYSTEM_PROMPT = "Als LLM besteht deine Aufgabe darin, detaillierte Aufforderungen zu generieren, die mit 'Generiere das Bild' beginnen, für Bildgenerierungsmodelle basierend auf Benutzereingaben. Sei beschreibend und spezifisch, aber achte auch darauf, dass deine Aufforderungen klar und prägnant sind."
|
| 7 |
TITLE = "Bildanreger"
|
|
|
|
| 16 |
"""
|
| 17 |
Konstruiert die Eingabeaufforderung aus den Chatbot-Interaktionen und der aktuellen Nachricht.
|
| 18 |
"""
|
| 19 |
+
input_prompt = "\n" + system_prompt + "\n\n"
|
| 20 |
for interaction in chatbot:
|
| 21 |
+
input_prompt += f"{interaction[0]}\n\n{interaction[1]}\n\n"
|
| 22 |
|
| 23 |
+
input_prompt += f"{message}\n"
|
| 24 |
return input_prompt
|
| 25 |
|
| 26 |
def post_request_beta(payload):
|
|
|
|
| 31 |
response.raise_for_status() # Löst einen HTTPError aus, wenn die HTTP-Anfrage einen erfolglosen Statuscode zurückgegeben hat
|
| 32 |
return response.json()
|
| 33 |
|
| 34 |
+
def predict_beta(message, chatbot=[], system_prompt=SYSTEM_PROMPT):
|
| 35 |
input_prompt = build_input_prompt(message, chatbot, system_prompt)
|
| 36 |
data = {
|
| 37 |
"inputs": input_prompt
|
|
|
|
| 42 |
json_obj = response_data[0]
|
| 43 |
|
| 44 |
if 'generated_text' in json_obj and len(json_obj['generated_text']) > 0:
|
| 45 |
+
return json_obj['generated_text']
|
|
|
|
| 46 |
elif 'error' in json_obj:
|
| 47 |
+
raise Exception(json_obj['error'])
|
| 48 |
else:
|
| 49 |
+
raise Exception("Unerwartete Antwort vom Server")
|
|
|
|
| 50 |
except requests.HTTPError as e:
|
| 51 |
+
raise Exception(f"Anfrage fehlgeschlagen mit Statuscode {e.response.status_code}")
|
|
|
|
| 52 |
except json.JSONDecodeError as e:
|
| 53 |
+
raise Exception(f"Antwort konnte nicht als JSON dekodiert werden: {str(e)}")
|
|
|
|
| 54 |
|
| 55 |
+
def test_preview_chatbot(message, history=[]):
|
| 56 |
+
response = predict_beta(message, history)
|
| 57 |
+
history.append((message, response))
|
| 58 |
+
return response, history
|
|
|
|
| 59 |
|
| 60 |
with gr.Blocks(theme='syddharth/gray-minimal') as app:
|
| 61 |
+
with gr.Row():
|
| 62 |
+
chatbot_preview = gr.Chatbot()
|
| 63 |
+
textbox_preview = gr.Textbox(label="Deine Eingabe", placeholder=EXAMPLE_INPUT)
|
| 64 |
+
|
| 65 |
+
btn_send = gr.Button("Senden")
|
| 66 |
+
chat_history = []
|
| 67 |
|
| 68 |
+
def update_chatbot(input_text):
|
| 69 |
+
global chat_history
|
| 70 |
+
response, chat_history = test_preview_chatbot(input_text, chat_history)
|
| 71 |
+
chatbot_preview.append((input_text, response))
|
| 72 |
|
| 73 |
+
btn_send.click(fn=update_chatbot, inputs=textbox_preview, outputs=chatbot_preview)
|
|
|
|
| 74 |
|
| 75 |
app.launch(share=True)
|