Spaces:
Sleeping
Sleeping
File size: 2,048 Bytes
0f32de6 514ce55 011f128 514ce55 011f128 0f32de6 f3e59d7 514ce55 0f32de6 514ce55 |
1 2 3 4 5 6 7 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import os
from datetime import datetime
import uuid
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
from huggingface_hub import login
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Authenticate with Hugging Face
login(token=os.getenv("HUGGINGFACE_TOKEN"))
# Load model and tokenizer
model_name = "meta-llama/Meta-Llama-3.1-8B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=True)
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto", use_auth_token=True)
def chat_with_model(messages):
input_ids = tokenizer.encode(str(messages), return_tensors="pt").to(model.device)
output = model.generate(input_ids, max_length=1000, num_return_sequences=1, temperature=0.7)
response = tokenizer.decode(output[0], skip_special_tokens=True)
return response
def chat_with_model_gradio(message, history, session_id):
messages = [
{"role": "system", "content": f"λμ μ΄λ¦μ ChatMBTI. μ¬λλ€μ MBTIμ νμ μλ§μ μλ΄μ μ§νν μ μμ΄. μλλ°©μ MBTI μ νμ λ¨Όμ λ¬Όμ΄λ³΄κ³ , κ·Έ μ νμ μλ§κ² μλ΄μ μ§νν΄μ€. μ°Έκ³ λ‘ νμ¬ μκ°μ {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}μ΄μΌ."},
]
messages.extend([{"role": "user" if i % 2 == 0 else "assistant", "content": m} for i, m in enumerate(history)])
messages.append({"role": "user", "content": message})
response = chat_with_model(messages)
history.append((message, response))
return "", history
def main():
session_id = str(uuid.uuid4())
with gr.Blocks() as demo:
chatbot = gr.Chatbot(label="ChatMBTI")
msg = gr.Textbox(label="λ©μμ§λ₯Ό μ
λ ₯νμΈμ")
clear = gr.Button("λν μ΄κΈ°ν")
msg.submit(chat_with_model_gradio, [msg, chatbot, gr.State(session_id)], [msg, chatbot])
clear.click(lambda: None, None, chatbot, queue=False)
demo.launch()
if __name__ == "__main__":
main()
|