daniloedu commited on
Commit
8c5a3b3
·
0 Parent(s):

Duplicate from daniloedu/chat_llm_v2

Browse files
Files changed (4) hide show
  1. .gitattributes +35 -0
  2. README.md +14 -0
  3. app.py +49 -0
  4. requirements.txt +7 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Chat Llm V2
3
+ emoji: 🦀
4
+ colorFrom: purple
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 3.39.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: apache-2.0
11
+ duplicated_from: daniloedu/chat_llm_v2
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import gradio as gr
4
+ from dotenv import load_dotenv
5
+ from transformers import AutoTokenizer
6
+
7
+ load_dotenv()
8
+
9
+ model_name = "tiiuae/falcon-7b-instruct"
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
+
12
+ API_URL = "https://api-inference.huggingface.co/models/tiiuae/falcon-7b-instruct"
13
+ headers = {"Authorization": f"Bearer {os.getenv('HF_API_KEY')}"}
14
+
15
+ def format_chat_prompt(message, instruction):
16
+ prompt = f"System:{instruction}\nUser: {message}\nAssistant:"
17
+ return prompt
18
+
19
+ def query(payload):
20
+ response = requests.post(API_URL, headers=headers, json=payload)
21
+ return response.json()
22
+
23
+ def respond(message, instruction="A conversation between a user and an AI assistant. The assistant gives helpful and honest answers."):
24
+ MAX_TOKENS = 1024 # limit for the model
25
+ prompt = format_chat_prompt(message, instruction)
26
+ # Check if the prompt is too long and, if so, truncate it
27
+ num_tokens = len(tokenizer.encode(prompt))
28
+ if num_tokens > MAX_TOKENS:
29
+ # Truncate the prompt to fit within the token limit
30
+ prompt = tokenizer.decode(tokenizer.encode(prompt)[-MAX_TOKENS:])
31
+
32
+ response = query({"inputs": prompt})
33
+ generated_text = response[0]['generated_text']
34
+ assistant_message = generated_text.split("Assistant:")[-1]
35
+ assistant_message = assistant_message.split("User:")[0].strip() # Only keep the text before the first "User:"
36
+ return assistant_message
37
+
38
+ iface = gr.Interface(
39
+ respond,
40
+ inputs=[
41
+ gr.inputs.Textbox(label="Your question"),
42
+ gr.inputs.Textbox(label="System message", lines=2, default="A conversation between a user and an AI assistant. The assistant gives helpful and honest answers.")
43
+ ],
44
+ outputs=[
45
+ gr.outputs.Textbox(label="AI's response")
46
+ ],
47
+ )
48
+
49
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ python-dotenv
2
+ gradio
3
+ transformers
4
+ torch
5
+ einops
6
+ accelerate
7
+ requests