Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
|
| 2 |
import gradio as gr
|
| 3 |
-
#from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 4 |
from ctransformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
-
import torch
|
| 6 |
import time
|
| 7 |
from datetime import datetime
|
| 8 |
import shutil
|
|
@@ -17,15 +15,12 @@ else:
|
|
| 17 |
print(f"π Folder already exists: {folder_path}")
|
| 18 |
|
| 19 |
# Define source file and destination folder
|
| 20 |
-
|
| 21 |
-
|
| 22 |
|
| 23 |
# Copy the file
|
| 24 |
-
shutil.copy(
|
| 25 |
-
|
| 26 |
-
source_file = "config.json"
|
| 27 |
-
# Copy the file
|
| 28 |
-
shutil.copy(source_file, destination_folder)
|
| 29 |
|
| 30 |
model_path = "./models/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf" # Adjust if necessary
|
| 31 |
|
|
@@ -35,37 +30,9 @@ else:
|
|
| 35 |
print(f"β Model not found at: {model_path}")
|
| 36 |
print("π Available files:", os.listdir("./models"))
|
| 37 |
|
| 38 |
-
torch.random.manual_seed(0)
|
| 39 |
-
"""
|
| 40 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 41 |
-
"microsoft/Phi-3-mini-4k-instruct",
|
| 42 |
-
device_map="cpu",
|
| 43 |
-
torch_dtype="auto",
|
| 44 |
-
trust_remote_code=True,
|
| 45 |
-
)
|
| 46 |
-
tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
|
| 47 |
-
"""
|
| 48 |
-
|
| 49 |
-
"""
|
| 50 |
-
model_name = "microsoft/phi-2"
|
| 51 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 52 |
-
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="cpu")
|
| 53 |
-
"""
|
| 54 |
-
|
| 55 |
-
torch.set_default_device("cpu")
|
| 56 |
-
|
| 57 |
print("model")
|
| 58 |
print(datetime.fromtimestamp(time.time()))
|
| 59 |
-
|
| 60 |
-
#model_name = "TheBloke/TinyLlama-1.1B-Chat-GGUF" # Change if using a different version
|
| 61 |
-
#model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0" # Change if using a different version
|
| 62 |
-
model_name = "microsoft/phi-1_5" # Change if using a different version
|
| 63 |
-
#model = AutoModelForCausalLM.from_pretrained(destination_folder, model_type="llama")
|
| 64 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 65 |
-
|
| 66 |
-
print("tokenizer")
|
| 67 |
-
print(datetime.fromtimestamp(time.time()))
|
| 68 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 69 |
|
| 70 |
print("done setup")
|
| 71 |
print(datetime.fromtimestamp(time.time()))
|
|
@@ -74,45 +41,8 @@ def generate_letter(date, letter_time, purpose, place, sender, receiver):
|
|
| 74 |
prompt = (f"Tuliskan surat resmi dengan detail sebagai berikut:\n"
|
| 75 |
f"Tanggal: {date}\nWaktu: {letter_time}\nTujuan: {purpose}\nTempat: {place}\n"
|
| 76 |
f"Pengirim: {sender}\nPenerima: {receiver}\n\nSurat:")
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
f"Sender: {sender}\nReceiver: {receiver}\n\nLetter:")"""
|
| 80 |
-
"""
|
| 81 |
-
messages = [
|
| 82 |
-
{"role": "system", "content": "You are a helpful AI assistant."},
|
| 83 |
-
{"role": "user", "content": f"{prompt}"},
|
| 84 |
-
]
|
| 85 |
-
|
| 86 |
-
pipe = pipeline(
|
| 87 |
-
"text-generation",
|
| 88 |
-
model=model,
|
| 89 |
-
tokenizer=tokenizer,
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
generation_args = {
|
| 93 |
-
"max_new_tokens": 500,
|
| 94 |
-
"return_full_text": False,
|
| 95 |
-
"temperature": 0.0,
|
| 96 |
-
"do_sample": False,
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
output = pipe(messages, **generation_args)
|
| 100 |
-
return output[0]['generated_text']
|
| 101 |
-
"""
|
| 102 |
-
|
| 103 |
-
print("tokenizer")
|
| 104 |
-
print(datetime.fromtimestamp(time.time()))
|
| 105 |
-
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 106 |
-
|
| 107 |
-
print("model.generate")
|
| 108 |
-
print(datetime.fromtimestamp(time.time()))
|
| 109 |
-
output = model.generate(**inputs, max_length=100, temperature=0.7, top_p=0.9)
|
| 110 |
-
|
| 111 |
-
print("return")
|
| 112 |
-
print(datetime.fromtimestamp(time.time()))
|
| 113 |
-
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 114 |
-
"""response = model(prompt)
|
| 115 |
-
return response"""
|
| 116 |
|
| 117 |
# Create the Gradio interface
|
| 118 |
iface = gr.Interface(
|
|
|
|
| 1 |
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
from ctransformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 4 |
import time
|
| 5 |
from datetime import datetime
|
| 6 |
import shutil
|
|
|
|
| 15 |
print(f"π Folder already exists: {folder_path}")
|
| 16 |
|
| 17 |
# Define source file and destination folder
|
| 18 |
+
source_file1 = "tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf"
|
| 19 |
+
source_file2 = "config.json"
|
| 20 |
|
| 21 |
# Copy the file
|
| 22 |
+
shutil.copy(source_file1, folder_path)
|
| 23 |
+
shutil.copy(source_file2, folder_path)
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
model_path = "./models/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf" # Adjust if necessary
|
| 26 |
|
|
|
|
| 30 |
print(f"β Model not found at: {model_path}")
|
| 31 |
print("π Available files:", os.listdir("./models"))
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
print("model")
|
| 34 |
print(datetime.fromtimestamp(time.time()))
|
| 35 |
+
model = AutoModelForCausalLM.from_pretrained(folder_path, model_type="llama")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
print("done setup")
|
| 38 |
print(datetime.fromtimestamp(time.time()))
|
|
|
|
| 41 |
prompt = (f"Tuliskan surat resmi dengan detail sebagai berikut:\n"
|
| 42 |
f"Tanggal: {date}\nWaktu: {letter_time}\nTujuan: {purpose}\nTempat: {place}\n"
|
| 43 |
f"Pengirim: {sender}\nPenerima: {receiver}\n\nSurat:")
|
| 44 |
+
response = model(prompt)
|
| 45 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Create the Gradio interface
|
| 48 |
iface = gr.Interface(
|