Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 7 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -18,6 +22,7 @@ def respond(
|
|
| 18 |
temperature,
|
| 19 |
top_p,
|
| 20 |
):
|
|
|
|
| 21 |
messages = [{"role": "system", "content": system_message}]
|
| 22 |
|
| 23 |
for val in history:
|
|
@@ -27,16 +32,26 @@ def respond(
|
|
| 27 |
messages.append({"role": "assistant", "content": val[1]})
|
| 28 |
|
| 29 |
messages.append({"role": "user", "content": message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
output_ids = model.generate(input_ids.to('cuda'))
|
| 33 |
-
respond = tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
|
| 34 |
-
|
| 35 |
-
return respond
|
| 36 |
|
| 37 |
-
|
| 38 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 39 |
-
"""
|
| 40 |
demo = gr.ChatInterface(
|
| 41 |
respond,
|
| 42 |
additional_inputs=[
|
|
@@ -51,4 +66,7 @@ demo = gr.ChatInterface(
|
|
| 51 |
label="Top-p (nucleus sampling)",
|
| 52 |
),
|
| 53 |
],
|
| 54 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
|
| 4 |
+
# Use a pipeline as a high-level helper for text generation
|
| 5 |
+
pipe = pipeline("text-generation", model="Wonder-Griffin/ShorseyBeerLeague")
|
| 6 |
+
|
| 7 |
+
# Assuming `model_path` is the Hugging Face model hub path or a local directory
|
| 8 |
+
model_path = "Wonder-Griffin/ShorseyBeerLeague" # Define this as needed
|
| 9 |
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 22 |
temperature,
|
| 23 |
top_p,
|
| 24 |
):
|
| 25 |
+
# Building the conversation history for the model
|
| 26 |
messages = [{"role": "system", "content": system_message}]
|
| 27 |
|
| 28 |
for val in history:
|
|
|
|
| 32 |
messages.append({"role": "assistant", "content": val[1]})
|
| 33 |
|
| 34 |
messages.append({"role": "user", "content": message})
|
| 35 |
+
|
| 36 |
+
# Tokenize the input message
|
| 37 |
+
input_text = " ".join([msg["content"] for msg in messages if msg["role"] == "user"])
|
| 38 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt")
|
| 39 |
+
|
| 40 |
+
# Generate a response from the model
|
| 41 |
+
output_ids = model.generate(
|
| 42 |
+
input_ids.to("cuda"),
|
| 43 |
+
max_new_tokens=max_tokens,
|
| 44 |
+
temperature=temperature,
|
| 45 |
+
top_p=top_p,
|
| 46 |
+
do_sample=True
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Decode the generated tokens into a response
|
| 50 |
+
response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 51 |
|
| 52 |
+
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Gradio interface setup
|
|
|
|
|
|
|
| 55 |
demo = gr.ChatInterface(
|
| 56 |
respond,
|
| 57 |
additional_inputs=[
|
|
|
|
| 66 |
label="Top-p (nucleus sampling)",
|
| 67 |
),
|
| 68 |
],
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
demo.launch()
|