Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,43 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load model and tokenizer from Hugging Face Model Hub
|
| 6 |
+
model_name = "meta-llama/Meta-Llama-3.1-70B-Instruct"
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Define system instruction
|
| 11 |
+
system_instruction = "You are a helpful assistant. Provide detailed and accurate responses to the user's queries."
|
| 12 |
+
|
| 13 |
+
# Define the chat function
|
| 14 |
+
def chat_function(prompt):
|
| 15 |
+
# Create the full input prompt including the system instruction
|
| 16 |
+
full_prompt = f"{system_instruction}\nUser: {prompt}\nAssistant:"
|
| 17 |
+
|
| 18 |
+
# Tokenize the full prompt
|
| 19 |
+
inputs = tokenizer(full_prompt, return_tensors="pt")
|
| 20 |
+
|
| 21 |
+
# Generate model response
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model.generate(**inputs, max_length=150, num_return_sequences=1)
|
| 24 |
+
|
| 25 |
+
# Decode and return response
|
| 26 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True).strip()
|
| 27 |
+
|
| 28 |
+
# Extract only the assistant's response
|
| 29 |
+
response = response.split("Assistant:")[-1].strip()
|
| 30 |
+
return response
|
| 31 |
+
|
| 32 |
+
# Create Gradio interface
|
| 33 |
+
iface = gr.Interface(
|
| 34 |
+
fn=chat_function,
|
| 35 |
+
inputs="text",
|
| 36 |
+
outputs="text",
|
| 37 |
+
title="Meta-Llama Chatbot",
|
| 38 |
+
description="A chatbot powered by the Meta-Llama-3.1-70B-Instruct model."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Launch the interface
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
iface.launch()
|