ManuelMonteiro commited on
Commit
c26d519
1 Parent(s): 26cd38c

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -82
app.py DELETED
@@ -1,82 +0,0 @@
1
- import json
2
- import gradio as gr
3
- import os
4
- import requests
5
-
6
- hf_token = os.getenv('HF_TOKEN')
7
- api_url = os.getenv('API_URL')
8
- headers = {
9
- 'Content-Type': 'application/json',
10
- }
11
-
12
- system_message = "\nYou are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.\n\nIf a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information."
13
- title = "Llama2 70B Chatbot"
14
- description = """
15
- This Space demonstrates model [Llama-2-70b-chat-hf](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) by Meta, a Llama 2 model with 70B parameters fine-tuned for chat instructions. This space is running on Inference Endpoints using text-generation-inference library. If you want to run your own service, you can also [deploy the model on Inference Endpoints](https://ui.endpoints.huggingface.co/).
16
-
17
- 🔎 For more details about the Llama 2 family of models and how to use them with `transformers`, take a look [at our blog post](https://huggingface.co/blog/llama2).
18
-
19
- 🔨 Looking for lighter chat model versions of Llama-v2?
20
- - 🐇 Check out the [7B Chat model demo](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat).
21
- - 🦊 Check out the [13B Chat model demo](https://huggingface.co/spaces/huggingface-projects/llama-2-13b-chat).
22
-
23
- Note: As a derivate work of [Llama-2-70b-chat](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) by Meta,
24
- this demo is governed by the original [license](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/LICENSE.txt) and [acceptable use policy](https://huggingface.co/spaces/ysharma/Explore_llamav2_with_TGI/blob/main/USE_POLICY.md).
25
- """
26
- css = """.toast-wrap { display: none !important } """
27
- examples=[
28
- 'Hello there! How are you doing?',
29
- 'Can you explain to me briefly what is Python programming language?',
30
- 'Explain the plot of Cinderella in a sentence.',
31
- 'How many hours does it take a man to eat a Helicopter?',
32
- "Write a 100-word article on 'Benefits of Open-Source in AI research'",
33
- ]
34
-
35
-
36
- def predict(message, chatbot):
37
-
38
- input_prompt = f"[INST] <<SYS>>\n{system_message}\n<</SYS>>\n\n "
39
- for interaction in chatbot:
40
- input_prompt = input_prompt + str(interaction[0]) + " [/INST] " + str(interaction[1]) + " </s><s> [INST] "
41
-
42
- input_prompt = input_prompt + str(message) + " [/INST] "
43
-
44
- data = {
45
- "inputs": input_prompt,
46
- "parameters": {"max_new_tokens":256}
47
- }
48
-
49
- response = requests.post(api_url, headers=headers, data=json.dumps(data), auth=('hf', hf_token), stream=True)
50
-
51
- partial_message = ""
52
- for line in response.iter_lines():
53
- if line: # filter out keep-alive new lines
54
- # Decode from bytes to string
55
- decoded_line = line.decode('utf-8')
56
-
57
- # Remove 'data:' prefix
58
- if decoded_line.startswith('data:'):
59
- json_line = decoded_line[5:] # Exclude the first 5 characters ('data:')
60
- else:
61
- gr.Warning(f"This line does not start with 'data:': {decoded_line}")
62
- continue
63
-
64
- # Load as JSON
65
- try:
66
- json_obj = json.loads(json_line)
67
- if 'token' in json_obj:
68
- partial_message = partial_message + json_obj['token']['text']
69
- yield partial_message
70
- elif 'error' in json_obj:
71
- yield json_obj['error'] + '. Please refresh and try again with an appropriate smaller input prompt.'
72
- else:
73
- gr.Warning(f"The key 'token' does not exist in this JSON object: {json_obj}")
74
-
75
- except json.JSONDecodeError:
76
- gr.Warning(f"This line is not valid JSON: {json_line}")
77
- continue
78
- except KeyError as e:
79
- gr.Warning(f"KeyError: {e} occurred for JSON object: {json_obj}")
80
- continue
81
-
82
- gr.ChatInterface(predict, title=title, description=description, css=css, examples=examples, cache_examples=True).queue(concurrency_count=75).launch()