Spaces:
Running
Running
Create app.py
Browse filesBasically the initial commit.
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
MAX_NEW_TOKENS = 600
|
| 8 |
+
|
| 9 |
+
# MODEL="HuggingFaceTB/SmolLM2-135M-Instruct"
|
| 10 |
+
# MODEL="HuggingFaceTB/SmolLM2-360M-Instruct"
|
| 11 |
+
MODEL="HuggingFaceTB/SmolLM2-1.7B-Instruct"
|
| 12 |
+
TEMPERATURE = 0.6
|
| 13 |
+
TOP_P = 0.95
|
| 14 |
+
REPETITION_PENALTY = 1.2
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
pipe = pipeline("text-generation", model="HuggingFaceTB/SmolLM2-1.7B-Instruct")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def message_fx(message, history):
|
| 22 |
+
if len(history) == 0:
|
| 23 |
+
send_to_api = [{'role':'user', 'content':message}]
|
| 24 |
+
print(send_to_api)
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
response = pipe(send_to_api,
|
| 27 |
+
do_sample=True,
|
| 28 |
+
max_new_tokens=MAX_NEW_TOKENS,
|
| 29 |
+
temperature=TEMPERATURE, # 1.0 = lots of creativity, high odd of hallucination 0.1 very specific writing and low odds
|
| 30 |
+
# top_k=50,
|
| 31 |
+
top_p=TOP_P,
|
| 32 |
+
repetition_penalty=REPETITION_PENALTY, # Added to discourage repetition
|
| 33 |
+
# no_repeat_ngram_size=3
|
| 34 |
+
)[0]['generated_text'][1]['content']
|
| 35 |
+
return response
|
| 36 |
+
|
| 37 |
+
else:
|
| 38 |
+
send_to_api = history + [{'role':'user', 'content':message}]
|
| 39 |
+
print(send_to_api)
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
response = pipe(send_to_api,
|
| 42 |
+
do_sample=True,
|
| 43 |
+
max_new_tokens=MAX_NEW_TOKENS,
|
| 44 |
+
temperature=TEMPERATURE, # 1.0 = lots of creativity, high odd of hallucination 0.1 very specific writing and low odds
|
| 45 |
+
# top_k=50,
|
| 46 |
+
top_p=TOP_P,
|
| 47 |
+
repetition_penalty=REPETITION_PENALTY, # Added to discourage repetition
|
| 48 |
+
# no_repeat_ngram_size=3
|
| 49 |
+
)[0]['generated_text'][-1]['content']
|
| 50 |
+
return response
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
gr.ChatInterface(
|
| 54 |
+
fn=message_fx,
|
| 55 |
+
type="messages"
|
| 56 |
+
).launch()
|
| 57 |
+
|