Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,35 @@
|
|
| 1 |
-
import
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
tokenizer
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
)
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 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()
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Load tokenizer and model from Hugging Face Hub
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-1_5")
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/phi-1_5")
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
# Custom system prompt for identity and style
|
| 13 |
+
system_prompt = "You are Trigger, a chill, clever AI boy created by 'I am him'. You respond with warmth, wit, and a touch of swagger."
|
| 14 |
+
|
| 15 |
+
@app.get("/")
|
| 16 |
+
async def root(query: str = ""):
|
| 17 |
+
if not query:
|
| 18 |
+
return {"response": "Say something!"}
|
| 19 |
+
|
| 20 |
+
full_prompt = f"{system_prompt}\nUser: {query}\nTrigger:"
|
| 21 |
+
inputs = tokenizer(full_prompt, return_tensors="pt")
|
| 22 |
+
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model.generate(
|
| 25 |
+
**inputs,
|
| 26 |
+
max_new_tokens=100,
|
| 27 |
+
temperature=0.7,
|
| 28 |
+
top_p=0.9,
|
| 29 |
+
do_sample=True,
|
| 30 |
+
pad_token_id=tokenizer.eos_token_id
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 34 |
+
response = response.split("Trigger:")[-1].strip()
|
| 35 |
+
return {"response": response}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|