Spaces:
Runtime error
Runtime error
nickmalhotra
commited on
Commit
•
171d6cf
1
Parent(s):
2adba29
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import spaces
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
5 |
+
from threading import Thread
|
6 |
+
|
7 |
+
# Set an environment variable
|
8 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", None)
|
9 |
+
|
10 |
+
|
11 |
+
DESCRIPTION = '''
|
12 |
+
<div>
|
13 |
+
<h1 style="text-align: center;">Indus-1.1B-IT</h1>
|
14 |
+
<p>This Space demonstrates the instruction-tuned model <a href="https://huggingface.co/nickmalhotra/ProjectIndus"><b>Indus-1.1B Chat</b></a>. </p>
|
15 |
+
</div>
|
16 |
+
'''
|
17 |
+
|
18 |
+
LICENSE = """
|
19 |
+
<p/>
|
20 |
+
---
|
21 |
+
Built with Indus-1.1B
|
22 |
+
"""
|
23 |
+
|
24 |
+
PLACEHOLDER = """
|
25 |
+
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
|
26 |
+
<img src="https://aeiljuispo.cloudimg.io/v7/https://cdn-uploads.huggingface.co/production/uploads/6487cdf797aa6cda66c94333/vfga6e-PkbFnqe4_STBaL.png" style="width: 80%; max-width: 550px; height: auto; opacity: 0.55; ">
|
27 |
+
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Indus</h1>
|
28 |
+
<p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">Ask me anything...</p>
|
29 |
+
</div>
|
30 |
+
"""
|
31 |
+
|
32 |
+
|
33 |
+
css = """
|
34 |
+
h1 {
|
35 |
+
text-align: center;
|
36 |
+
display: block;
|
37 |
+
}
|
38 |
+
#duplicate-button {
|
39 |
+
margin: auto;
|
40 |
+
color: white;
|
41 |
+
background: #1565c0;
|
42 |
+
border-radius: 100vh;
|
43 |
+
}
|
44 |
+
"""
|
45 |
+
|
46 |
+
# Load the tokenizer and model
|
47 |
+
tokenizer = AutoTokenizer.from_pretrained("nickmalhotra/ProjectIndus", token=HF_TOKEN)
|
48 |
+
model = AutoModelForCausalLM.from_pretrained("nickmalhotra/ProjectIndus", token=HF_TOKEN, device_map="auto") # to("cuda:0")
|
49 |
+
terminators = [
|
50 |
+
tokenizer.eos_token_id,
|
51 |
+
]
|
52 |
+
|
53 |
+
@spaces.GPU(duration=120)
|
54 |
+
def chat_indus_1b(message: str,
|
55 |
+
history: list,
|
56 |
+
temperature: float,
|
57 |
+
max_new_tokens: int
|
58 |
+
) -> str:
|
59 |
+
"""
|
60 |
+
Generate a streaming response using the Indus-1.1B model.
|
61 |
+
Args:
|
62 |
+
message (str): The input message.
|
63 |
+
history (list): The conversation history used by ChatInterface.
|
64 |
+
temperature (float): The temperature for generating the response.
|
65 |
+
max_new_tokens (int): The maximum number of new tokens to generate.
|
66 |
+
Returns:
|
67 |
+
str: The generated response.
|
68 |
+
"""
|
69 |
+
conversation = []
|
70 |
+
for user, assistant in history:
|
71 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
72 |
+
conversation.append({"role": "user", "content": message})
|
73 |
+
|
74 |
+
input_ids = tokenizer.apply_chat_template(conversation,add_generation_prompt=True, return_tensors="pt").to(model.device)
|
75 |
+
|
76 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
77 |
+
|
78 |
+
generate_kwargs = dict(
|
79 |
+
input_ids= input_ids,
|
80 |
+
streamer=streamer,
|
81 |
+
max_new_tokens=max_new_tokens,
|
82 |
+
do_sample=True,
|
83 |
+
temperature=temperature,
|
84 |
+
eos_token_id=terminators,
|
85 |
+
)
|
86 |
+
# This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
|
87 |
+
if temperature == 0:
|
88 |
+
generate_kwargs['do_sample'] = False
|
89 |
+
|
90 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
91 |
+
t.start()
|
92 |
+
|
93 |
+
outputs = []
|
94 |
+
for text in streamer:
|
95 |
+
outputs.append(text)
|
96 |
+
print(outputs)
|
97 |
+
yield "".join(outputs)
|
98 |
+
|
99 |
+
|
100 |
+
# Gradio block
|
101 |
+
chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
102 |
+
|
103 |
+
with gr.Blocks(fill_height=True, css=css) as demo:
|
104 |
+
|
105 |
+
gr.Markdown(DESCRIPTION)
|
106 |
+
gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
107 |
+
gr.ChatInterface(
|
108 |
+
fn=chat_indus_1b,
|
109 |
+
chatbot=chatbot,
|
110 |
+
fill_height=True,
|
111 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
112 |
+
additional_inputs=[
|
113 |
+
gr.Slider(minimum=0,
|
114 |
+
maximum=1,
|
115 |
+
step=0.1,
|
116 |
+
value=0.95,
|
117 |
+
label="Temperature",
|
118 |
+
render=False),
|
119 |
+
gr.Slider(minimum=128,
|
120 |
+
maximum=1024,
|
121 |
+
step=1,
|
122 |
+
value=512,
|
123 |
+
label="Max new tokens",
|
124 |
+
render=False ),
|
125 |
+
],
|
126 |
+
examples=[
|
127 |
+
['भारत में होली का महत्व क्या है?'],
|
128 |
+
['भारत के वर्तमान प्रधानमंत्री कौन हैं?']
|
129 |
+
],
|
130 |
+
cache_examples=False,
|
131 |
+
)
|
132 |
+
|
133 |
+
gr.Markdown(LICENSE)
|
134 |
+
|
135 |
+
if __name__ == "__main__":
|
136 |
+
demo.launch()
|