Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,33 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import necessary libraries
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Load the model and tokenizer
|
6 |
+
model_name = "meta-llama/Llama-2-7b-chat-hf"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
+
|
10 |
+
# Define the chat function
|
11 |
+
def chat_with_llama2(input_text):
|
12 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
13 |
+
outputs = model.generate(inputs["input_ids"], max_length=512, do_sample=True, top_p=0.95, top_k=60)
|
14 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
15 |
+
return response
|
16 |
+
|
17 |
+
# Create the Gradio interface
|
18 |
+
interface = gr.Interface(
|
19 |
+
fn=chat_with_llama2,
|
20 |
+
inputs="text",
|
21 |
+
outputs="text",
|
22 |
+
title="LLaMa 2 Chat HF",
|
23 |
+
description="Chat with LLaMa 2 model using Hugging Face Transformers and Gradio.",
|
24 |
+
examples=[
|
25 |
+
["Hello, LLaMa 2! How are you today?"],
|
26 |
+
["Can you tell me a joke?"],
|
27 |
+
["What is the capital of France?"]
|
28 |
+
]
|
29 |
+
)
|
30 |
+
|
31 |
+
# Launch the interface
|
32 |
+
if __name__ == "__main__":
|
33 |
+
interface.launch()
|