AFischer1985 commited on
Commit
584cd0c
·
verified ·
1 Parent(s): b568762

Update run.py

Browse files
Files changed (1) hide show
  1. run.py +82 -46
run.py CHANGED
@@ -1,53 +1,89 @@
1
- #############################################################################
2
- # Title: Gradio Interface to AI hosted by Huggingface
3
  # Author: Andreas Fischer
4
- # Date: October 7th, 2023
5
- # Last update: December 29th, 2023
6
- #############################################################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  import gradio as gr
9
- import requests
10
- import time
11
- import json
12
-
13
- def response(message, history, model):
14
- if(model=="Default"): model = "mistralai/Mixtral-8x7B-Instruct-v0.1"
15
- model_id = model
16
- params={"max_new_tokens":600, "return_full_text":False} #, "max_length":500, "stream":True
17
- url = f"https://api-inference.huggingface.co/models/{model_id}"
18
- correction=1
19
- prompt=f"[INST] {message} [/INST]" # skipped <s>
20
- print("URL: "+url)
21
- print(params)
22
- print("User: "+message+"\nAI: ")
23
- response=""
24
- for text in requests.post(url, json={"inputs":prompt, "parameters":params}, stream=True):
25
- text=text.decode('UTF-8')
26
- print(text)
27
- if(correction==3):
28
- text='"}]'+text
29
- correction=2
30
- if(correction==1):
31
- text=text.lstrip('[{"generated_text":"')
32
- correction=2
33
- if(text.endswith('"}]')):
34
- text=text.rstrip('"}]')
35
- correction=3
36
- response=response+text
37
- print(text)
38
- time.sleep(0.2)
39
  yield response
40
 
41
- x=requests.get(f"https://api-inference.huggingface.co/framework/text-generation-inference")
42
- x=[i["model_id"] for i in x.json()]
43
- print(x)
44
- x=[s for s in x if s.startswith("mistral")]
45
- print(x)
46
- x.insert(0,"Default")
47
-
48
- gr.ChatInterface(
49
- response,
50
- title="AI-Interface to HuggingFace-Models",
51
- additional_inputs=[gr.Dropdown(x,value="Default",label="Model")]).queue().launch(share=True) #False, server_name="0.0.0.0", server_port=7864)
52
 
 
 
 
 
 
 
 
 
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #########################################################################################
2
+ # Title: Gradio Chatbot Demo
3
  # Author: Andreas Fischer
4
+ # Date: June 22nd, 2024
5
+ # Last update: June 22nd, 2024
6
+ ##########################################################################################
7
+
8
+ myToken=None
9
+
10
+ # Specify Prompt Formating
11
+ #---------------------------
12
+
13
+ import re
14
+ def format_prompt(message="", history=None, system=None, RAGAddon=None, system2=None, zeichenlimit=None,historylimit=4, removeHTML=True):
15
+ if zeichenlimit is None: zeichenlimit=1000000000 # :-)
16
+ startOfString="<s>" # ""
17
+ template0=" [INST] {system} [/INST] </s>" # "<BOS_TOKEN><|START_OF_TURN_TOKEN|><|SYSTEM_TOKEN|> {system}<|END_OF_TURN_TOKEN|>"
18
+ template1=" [INST] {message} [/INST]" # "<|START_OF_TURN_TOKEN|><|USER_TOKEN|>{message}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>"
19
+ template2=" {response}</s>" # "{response}<|END_OF_TURN_TOKEN|>"
20
+ prompt = "" # Prompt is built dynamically from components:
21
+ if RAGAddon is not None:
22
+ system += RAGAddon
23
+ if system is not None:
24
+ prompt += template0.format(system=system)
25
+ message=message.replace("[INST]","")
26
+ message=message.replace("[/INST]","")
27
+ message=message.replace("</s>","")
28
+ message=re.sub("<[|](im_start|im_end|end_of_turn)[|]>", '', message)
29
+ if history is not None:
30
+ for user_message, bot_response in history[-historylimit:]:
31
+ if user_message is None: user_message = ""
32
+ if bot_response is None: bot_response = ""
33
+ if removeHTML==True: bot_response = re.sub("<(.*?)>","\n", bot_response) # remove HTML-components in general (may cause bugs with markdown-rendering)
34
+ if user_message is not None: prompt += template1.format(message=user_message[:zeichenlimit])
35
+ if bot_response is not None: prompt += template2.format(response=bot_response[:zeichenlimit])
36
+ if message is not None: prompt += template1.format(message=message[:zeichenlimit])
37
+ if system2 is not None:
38
+ prompt += system2
39
+ return startOfString+prompt
40
+
41
+
42
+ # Specify Chatbot Response
43
+ #--------------------------
44
 
45
  import gradio as gr
46
+ def response(message, history,system, hfToken):
47
+ global client
48
+ if(hfToken.startswith("hf_")): # use HF-hub with custom token if token is provided
49
+ client = InferenceClient(model=myModel, token=hfToken)
50
+ if(system==""): # set default system prompt is no system prompt is provided
51
+ system="Du bist ein hilfsbereiter Chatbot und antwortest bevorzugt in deutscher Sprache."
52
+ prompt=format_prompt(
53
+ message, # current message of the user (str)
54
+ history, # complete history (list)
55
+ system # system prompt (str)
56
+ )
57
+ print(prompt)
58
+ generate_kwargs = dict(temperature=float(0.9), max_new_tokens=1000, top_p=float(0.95), repetition_penalty=1.0, do_sample=True, seed=42)
59
+ stream = client.text_generation(prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
60
+ response = ""
61
+ for text in stream: # stream response token by token
62
+ part=text.token.text
63
+ response += part
64
+ print(part,end="", flush=True)
65
+ #response = re.sub("<(.*?)>","\n", response) # remove HTML-components in general (may cause bugs with markdown-rendering)
 
 
 
 
 
 
 
 
 
 
66
  yield response
67
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ # Connect to Model on the Huggingface Hub
70
+ #-----------------------------------------
71
+ from huggingface_hub import InferenceClient
72
+ myModel="mistralai/Mixtral-8x7B-Instruct-v0.1"
73
+ client = InferenceClient(
74
+ model=myModel,
75
+ token=myToken #token="hf_..."
76
+ )
77
 
78
+ # Start Gradio-User-Interace
79
+ #---------------------------
80
+ gr.ChatInterface(
81
+ response,
82
+ chatbot=gr.Chatbot(value=None, render_markdown=True),
83
+ title="Gradio Chatbot Demo",
84
+ additional_inputs=[
85
+ gr.Textbox(label="System Prompt",value="Du bist ein hilfsbereiter Chatbot und antwortest bevorzugt in deutscher Sprache."),
86
+ gr.Textbox(label="HF_token",value="")
87
+ ]
88
+ ).queue().launch(share=True) #False, server_name="0.0.0.0", server_port=7864)
89
+ print("Interface up and running!")