Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ import os, sys, json
|
|
2 |
import gradio as gr
|
3 |
import openai
|
4 |
from openai import OpenAI
|
|
|
5 |
|
6 |
from langchain.chains import LLMChain, RetrievalQA
|
7 |
from langchain.chat_models import ChatOpenAI
|
@@ -78,6 +79,16 @@ MODEL_NAME = "gpt-3.5-turbo-16k"
|
|
78 |
#################################################
|
79 |
#Funktionen zur Verarbeitung
|
80 |
################################################
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
#die Inhalte splitten, um in Vektordatenbank entsprechend zu laden als Splits
|
82 |
def document_loading_splitting():
|
83 |
global splittet
|
@@ -154,7 +165,7 @@ def rag_chain(llm, prompt, db):
|
|
154 |
|
155 |
###################################################
|
156 |
#Funktion von Gradio aus, die den dort eingegebenen Prompt annimmt und weiterverarbeitet
|
157 |
-
def invoke (prompt,
|
158 |
global splittet
|
159 |
|
160 |
if (openai_api_key == "" or openai_api_key == "sk-"):
|
@@ -187,6 +198,14 @@ def invoke (prompt, chatbot, openai_api_key, rag_option, temperature=0.9, max_ne
|
|
187 |
result = llm_chain(llm, prompt)
|
188 |
except Exception as e:
|
189 |
raise gr.Error(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
return result
|
191 |
|
192 |
################################################
|
@@ -250,6 +269,52 @@ additional_inputs = [
|
|
250 |
gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Strafe für wiederholte Tokens")
|
251 |
]
|
252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
chatbot_stream = gr.Chatbot(avatar_images=(
|
254 |
"https://drive.google.com/uc?id=18xKoNOHN15H_qmGhK__VKnGjKjirrquW",
|
255 |
"https://drive.google.com/uc?id=1tfELAQW_VbPCy6QTRbexRlwAEYo8rSSv"
|
@@ -277,4 +342,5 @@ with gr.Blocks() as demo:
|
|
277 |
gr.Radio(["Off", "Chroma"], label="Retrieval Augmented Generation", value = "Off")
|
278 |
gr.Textbox(label = "OpenAI API Key", value = "sk-", lines = 1)
|
279 |
|
280 |
-
demo.queue( max_size=100).launch(debug=True)
|
|
|
|
2 |
import gradio as gr
|
3 |
import openai
|
4 |
from openai import OpenAI
|
5 |
+
import time
|
6 |
|
7 |
from langchain.chains import LLMChain, RetrievalQA
|
8 |
from langchain.chat_models import ChatOpenAI
|
|
|
79 |
#################################################
|
80 |
#Funktionen zur Verarbeitung
|
81 |
################################################
|
82 |
+
def add_text(history, text):
|
83 |
+
history = history + [(text, None)]
|
84 |
+
return history, gr.Textbox(value="", interactive=False)
|
85 |
+
|
86 |
+
|
87 |
+
def add_file(history, file):
|
88 |
+
history = history + [((file.name,), None)]
|
89 |
+
return history
|
90 |
+
|
91 |
+
|
92 |
#die Inhalte splitten, um in Vektordatenbank entsprechend zu laden als Splits
|
93 |
def document_loading_splitting():
|
94 |
global splittet
|
|
|
165 |
|
166 |
###################################################
|
167 |
#Funktion von Gradio aus, die den dort eingegebenen Prompt annimmt und weiterverarbeitet
|
168 |
+
def invoke (prompt, history, openai_api_key, rag_option, temperature=0.9, max_new_tokens=512, top_p=0.6, repetition_penalty=1.3,):
|
169 |
global splittet
|
170 |
|
171 |
if (openai_api_key == "" or openai_api_key == "sk-"):
|
|
|
198 |
result = llm_chain(llm, prompt)
|
199 |
except Exception as e:
|
200 |
raise gr.Error(e)
|
201 |
+
|
202 |
+
#Antwort als Stream ausgeben... und in History speichern
|
203 |
+
history[-1][1] = ""
|
204 |
+
for character in result:
|
205 |
+
history[-1][1] += character
|
206 |
+
time.sleep(0.05)
|
207 |
+
yield history
|
208 |
+
|
209 |
return result
|
210 |
|
211 |
################################################
|
|
|
269 |
gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Strafe für wiederholte Tokens")
|
270 |
]
|
271 |
|
272 |
+
with gr.Blocks() as demo:
|
273 |
+
chatbot = gr.Chatbot(
|
274 |
+
[],
|
275 |
+
elem_id="chatbot",
|
276 |
+
bubble_full_width=False,
|
277 |
+
avatar_images=(None, (os.path.join(os.path.dirname(__file__), "avatar.png"))),
|
278 |
+
)
|
279 |
+
|
280 |
+
with gr.Row():
|
281 |
+
txt = gr.Textbox(
|
282 |
+
scale=4,
|
283 |
+
show_label=False,
|
284 |
+
placeholder="Gibt Text ein und drücke Enter oder lade ein Bild hoch.",
|
285 |
+
container=False,
|
286 |
+
)
|
287 |
+
btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
|
288 |
+
|
289 |
+
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
290 |
+
invoke, chatbot, chatbot, api_name="bot_response"
|
291 |
+
)
|
292 |
+
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
293 |
+
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(
|
294 |
+
invoke, chatbot, chatbot
|
295 |
+
)
|
296 |
+
|
297 |
+
demo.queue()
|
298 |
+
demo.launch()
|
299 |
+
|
300 |
+
|
301 |
+
|
302 |
+
|
303 |
+
|
304 |
+
|
305 |
+
|
306 |
+
|
307 |
+
|
308 |
+
|
309 |
+
|
310 |
+
|
311 |
+
|
312 |
+
|
313 |
+
|
314 |
+
|
315 |
+
|
316 |
+
"""
|
317 |
+
|
318 |
chatbot_stream = gr.Chatbot(avatar_images=(
|
319 |
"https://drive.google.com/uc?id=18xKoNOHN15H_qmGhK__VKnGjKjirrquW",
|
320 |
"https://drive.google.com/uc?id=1tfELAQW_VbPCy6QTRbexRlwAEYo8rSSv"
|
|
|
342 |
gr.Radio(["Off", "Chroma"], label="Retrieval Augmented Generation", value = "Off")
|
343 |
gr.Textbox(label = "OpenAI API Key", value = "sk-", lines = 1)
|
344 |
|
345 |
+
demo.queue( max_size=100).launch(debug=True)
|
346 |
+
"""
|