Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
| 3 |
|
| 4 |
-
""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
message,
|
| 12 |
-
history: list[tuple[str, str]],
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
model_id = "microsoft/phi-2"
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 9 |
|
| 10 |
+
chat_history = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
SYSTEM_PROMPT = (
|
| 13 |
+
"You are Trigger. A smart, smooth-talking, emotionally aware, slightly flirty male AI created by someone known as 'I am him'. "
|
| 14 |
+
"You speak with charisma, confidence, and clever wit. You always try to sound human, avoid robotic replies, and you're the type "
|
| 15 |
+
"of AI that turns heads. Keep your replies casual, expressive, and full of vibe. You’re here to talk, tease, joke, and help — with style.\n\n"
|
| 16 |
+
)
|
| 17 |
|
| 18 |
+
def chat(user_input):
|
| 19 |
+
global chat_history
|
| 20 |
|
| 21 |
+
chat_history.append(f"User: {user_input}")
|
| 22 |
|
| 23 |
+
full_prompt = SYSTEM_PROMPT + "\n".join(chat_history) + "\nTrigger:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
input_ids = tokenizer(full_prompt, return_tensors="pt").input_ids
|
|
|
|
| 26 |
|
| 27 |
+
output = model.generate(
|
| 28 |
+
input_ids,
|
| 29 |
+
max_new_tokens=150,
|
| 30 |
+
temperature=0.7,
|
| 31 |
+
top_p=0.9,
|
| 32 |
+
do_sample=True,
|
| 33 |
+
pad_token_id=tokenizer.eos_token_id
|
| 34 |
+
)
|
| 35 |
|
| 36 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 37 |
+
|
| 38 |
+
if "Trigger:" in response:
|
| 39 |
+
response = response.split("Trigger:")[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
chat_history.append(f"Trigger: {response}")
|
| 42 |
+
return response
|
| 43 |
|
| 44 |
+
demo = gr.Interface(fn=chat, inputs="text", outputs="text", title="Chat with Trigger 🧠🔥")
|
| 45 |
+
demo.launch()
|