Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from peft import PeftModel, PeftConfig
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
|
@@ -41,7 +42,6 @@ class Conversation:
|
|
| 41 |
final_text += DEFAULT_RESPONSE_TEMPLATE
|
| 42 |
return final_text.strip()
|
| 43 |
|
| 44 |
-
|
| 45 |
def generate(model, tokenizer, prompt, generation_config):
|
| 46 |
data = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
|
| 47 |
data = {k: v.to(model.device) for k, v in data.items()}
|
|
@@ -69,17 +69,22 @@ model.eval()
|
|
| 69 |
|
| 70 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=False)
|
| 71 |
generation_config = GenerationConfig.from_pretrained(MODEL_NAME)
|
| 72 |
-
print(generation_config)
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
conversation = Conversation()
|
| 77 |
-
conversation.add_user_message(
|
| 78 |
prompt = conversation.get_prompt(tokenizer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
print(inp)
|
| 82 |
-
print(output)
|
| 83 |
-
print()
|
| 84 |
-
print("==============================")
|
| 85 |
-
print()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from peft import PeftModel, PeftConfig
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig
|
|
|
|
| 42 |
final_text += DEFAULT_RESPONSE_TEMPLATE
|
| 43 |
return final_text.strip()
|
| 44 |
|
|
|
|
| 45 |
def generate(model, tokenizer, prompt, generation_config):
|
| 46 |
data = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
|
| 47 |
data = {k: v.to(model.device) for k, v in data.items()}
|
|
|
|
| 69 |
|
| 70 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, use_fast=False)
|
| 71 |
generation_config = GenerationConfig.from_pretrained(MODEL_NAME)
|
|
|
|
| 72 |
|
| 73 |
+
# Gradio interface setup
|
| 74 |
+
def chat_with_model(user_input):
|
| 75 |
conversation = Conversation()
|
| 76 |
+
conversation.add_user_message(user_input)
|
| 77 |
prompt = conversation.get_prompt(tokenizer)
|
| 78 |
+
response = generate(model, tokenizer, prompt, generation_config)
|
| 79 |
+
conversation.add_bot_message(response)
|
| 80 |
+
return conversation.messages[-1]["content"]
|
| 81 |
+
|
| 82 |
+
iface = gr.Interface(
|
| 83 |
+
fn=chat_with_model,
|
| 84 |
+
inputs=gr.Textbox(prompt="You:"),
|
| 85 |
+
outputs=gr.Textbox(prompt="Bot:"),
|
| 86 |
+
live=True,
|
| 87 |
+
title="Chat with Bot",
|
| 88 |
+
)
|
| 89 |
|
| 90 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|