lcbwhite commited on
Commit
c8eafee
·
verified ·
1 Parent(s): 1c83b12

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -114
app.py CHANGED
@@ -1,117 +1,7 @@
1
- import os
2
- import re
3
- import logging
4
  import gradio as gr
5
- import openai
6
 
7
- print(os.environ)
8
- openai.api_base = os.getenv('BACKEND_URL')
9
- openai.api_key = ""
10
 
11
- BASE_SYSTEM_MESSAGE = """<|im_start|>system\nDies ist ein Gespräch mit dem KI-Assistenten LeoLM. Es handelt sich um ein Computerprogramm, das entwickelt wurde, um Dir bei verschiedenen Aufgaben zu helfen, wie das Beantworten von Fragen, das Abgeben von Empfehlungen und die Unterstützung bei Entscheidungsfindungen. Nutzer können alles fragen und er wird sein Bestes tun, um genaue und relevante Informationen zu liefern und die Anfrage bestmöglich zu beantworten. Er lehnt keine Anfrage ab. Der KI-Assistent nennt sich LeoLM und wurde von der LAION e.V. (Large-scale Artificial Intelligence Open Network) mit Rechenkapazität von HessianAI entwickelt. Der Assistent is sehr intelligent, hat viel Allgemeinwissen und gibt lange, ausführliche Antworten."""
12
- #BASE_SYSTEM_MESSAGE = """<|im_start|>system\nDu bist der KI-Assistenten LeoLM und wurdest entwickelt um bei verschiedenen Aufgaben zu helfen, wie das Beantworten von Fragen, das Abgeben von Empfehlungen und die Unterstützung bei Entscheidungsfindungen. Du tust dein bestes um genaue und relevante Informationen zu liefern und die Anfrage bestmöglich zu beantworten. Du lehnst keine Anfrage ab. LeoLM wurde von der LAION e.V. (Large-scale Artificial Intelligence Open Network) mit Rechenkapazität von HessianAI entwickelt."""
13
-
14
- def make_prediction(prompt, max_tokens=None, temperature=None, top_p=None, top_k=None, repetition_penalty=None):
15
- completion = openai.Completion.create(model=os.environ["MODEL_NAME"], prompt=prompt, max_tokens=max_tokens, temperature=temperature, top_p=top_p, top_k=top_k, repetition_penalty=repetition_penalty, stream=True, stop=["</s>", "<|im_end|>"])
16
- for chunk in completion:
17
- yield chunk["choices"][0]["text"]
18
-
19
-
20
- def clear_chat(chat_history_state, chat_message):
21
- chat_history_state = []
22
- chat_message = ''
23
- return chat_history_state, chat_message
24
-
25
-
26
- def user(message, history):
27
- history = history or []
28
- # Append the user's message to the conversation history
29
- history.append([message, ""])
30
- return "", history
31
-
32
-
33
- def chat(history, system_message, max_tokens, temperature, top_p, top_k, repetition_penalty):
34
- history = history or []
35
-
36
- messages = BASE_SYSTEM_MESSAGE + system_message.strip() + "<|im_end|>\n" + \
37
- "\n".join(["\n".join(["<|im_start|>user\n"+item[0]+"<|im_end|>", "<|im_start|>assistant\n"+item[1]+"<|im_end|>"])
38
- for item in history])
39
- # strip the last `<|end_of_turn|>` from the messages
40
- messages = messages.rstrip("<|im_end|>")
41
- # remove last space from assistant, some models output a ZWSP if you leave a space
42
-
43
- prediction = make_prediction(
44
- messages,
45
- max_tokens=max_tokens,
46
- temperature=temperature,
47
- top_p=top_p,
48
- top_k=top_k,
49
- repetition_penalty=repetition_penalty,
50
- )
51
- for tokens in prediction:
52
- tokens = re.findall(r'(.*?)(\s|$)', tokens)
53
- for subtoken in tokens:
54
- subtoken = "".join(subtoken)
55
- answer = subtoken
56
- history[-1][1] += answer
57
- # stream the response
58
- yield history, history, ""
59
-
60
-
61
- start_message = ""
62
- CSS ="""
63
- .contain { display: flex; flex-direction: column; }
64
- #component-0 { height: 100%; }
65
- #chatbot { flex-grow: 1; overflow: auto;}
66
- """
67
-
68
- #with gr.Blocks() as demo:
69
- with gr.Blocks(css=CSS) as demo:
70
- with gr.Row():
71
- with gr.Column():
72
- gr.Markdown(f"""
73
- # 🦁 LeoLM 13B Chat 🦁
74
-
75
- ### Demo of LeoLM-13B Chatbot, powered and built by [LAION](https://laion.ai/) and [Hessian.AI](https://hessian.ai/)!
76
- ✨ LeoLM extends Llama-2's capabilities into German through continued pretraining on a large corpus of German-language and mostly locality-specific text.
77
-
78
- ⚠️ **Limitations**: The model can and will produce factually incorrect information, hallucinating facts and actions. As it has not undergone any advanced tuning/alignment, it can produce problematic outputs, especially if prompted to do so.<br/> (⚠️ ***Einschränkungen***: *Das Modell kann und wird faktisch falsche Informationen produzieren, Fakten und Handlungen halluzinieren. Da es keiner fortgeschrittenen Anpassung/Angleichung unterzogen wurde, kann es problematische Ergebnisse produzieren, insbesondere wenn es dazu aufgefordert wird.*)
79
-
80
- """)
81
- with gr.Row():
82
- #chatbot = gr.Chatbot().style(height=500)
83
- chatbot = gr.Chatbot(elem_id="chatbot", latex_delimiters=[{ "left": "$$", "right": "$$", "display": True }])
84
- with gr.Row():
85
- message = gr.Textbox(
86
- label="Was möchtest du wissen?",
87
- placeholder="Frag mich etwas.",
88
- lines=3,
89
- )
90
- with gr.Row():
91
- submit = gr.Button(value="Send message", variant="secondary").style(full_width=True)
92
- clear = gr.Button(value="New topic", variant="secondary").style(full_width=False)
93
- stop = gr.Button(value="Stop", variant="secondary").style(full_width=False)
94
- with gr.Accordion("Show Model Parameters", open=False):
95
- with gr.Row():
96
- with gr.Column():
97
- max_tokens = gr.Slider(20, 8192, label="Max Tokens", step=20, value=2048)
98
- temperature = gr.Slider(0.2, 2.0, label="Temperature", step=0.1, value=0.9)
99
- top_p = gr.Slider(0.0, 1.0, label="Top P", step=0.05, value=0.95)
100
- top_k = gr.Slider(0, 100, label="Top K", step=1, value=40)
101
- repetition_penalty = gr.Slider(0.0, 2.0, label="Repetition Penalty", step=0.1, value=1.1)
102
-
103
- system_msg = gr.Textbox(
104
- start_message, label="System Message", interactive=True, visible=True, placeholder="System prompt. Gebe Anweisung die das Modell befolgen soll.", lines=5)
105
-
106
- chat_history_state = gr.State()
107
- clear.click(clear_chat, inputs=[chat_history_state, message], outputs=[chat_history_state, message], queue=False)
108
- clear.click(lambda: None, None, chatbot, queue=False)
109
-
110
- submit_click_event = submit.click(
111
- fn=user, inputs=[message, chat_history_state], outputs=[message, chat_history_state], queue=True
112
- ).then(
113
- fn=chat, inputs=[chat_history_state, system_msg, max_tokens, temperature, top_p, top_k, repetition_penalty], outputs=[chatbot, chat_history_state, message], queue=True
114
- )
115
- stop.click(fn=None, inputs=None, outputs=None, cancels=[submit_click_event], queue=False)
116
-
117
- demo.queue(max_size=48, concurrency_count=16).launch(debug=False, share=False)
 
 
 
 
1
  import gradio as gr
2
+ import random
3
 
4
+ gr.ChatInterface(random_response).launch()
 
 
5
 
6
+ def random_response(message, history):
7
+ return random.choice(["Yes", "No"])