Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 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 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
],
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Replace with your actual model ID
|
| 6 |
+
model_id = "alphaoumardev/Llama3-8B-noryu-instruct"
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 10 |
+
model.eval()
|
| 11 |
+
|
| 12 |
+
# If you're using GPU on HF Spaces with GPU enabled
|
| 13 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 14 |
+
model.to(device)
|
| 15 |
+
|
| 16 |
+
def chat(user_input, history=[]):
|
| 17 |
+
# Add user input to the history
|
| 18 |
+
history.append({"role": "user", "content": user_input})
|
| 19 |
+
|
| 20 |
+
# Format prompt (adjust as needed depending on your training)
|
| 21 |
+
prompt = ""
|
| 22 |
+
for turn in history:
|
| 23 |
+
role = turn["role"]
|
| 24 |
+
content = turn["content"]
|
| 25 |
+
prompt += f"{role}: {content}\n"
|
| 26 |
+
prompt += "assistant:"
|
| 27 |
+
|
| 28 |
+
# Tokenize and generate
|
| 29 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
outputs = model.generate(
|
| 32 |
+
**inputs,
|
| 33 |
+
max_new_tokens=200,
|
| 34 |
+
do_sample=True,
|
| 35 |
+
temperature=0.7,
|
| 36 |
+
top_p=0.9,
|
| 37 |
+
pad_token_id=tokenizer.eos_token_id
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
output_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
+
|
| 42 |
+
# Extract just the assistant's reply
|
| 43 |
+
assistant_reply = output_text.split("assistant:")[-1].strip()
|
| 44 |
+
history.append({"role": "assistant", "content": assistant_reply})
|
| 45 |
+
|
| 46 |
+
# Return response and updated history for Gradio
|
| 47 |
+
chat_history = [(h["content"], history[i + 1]["content"]) for i, h in enumerate(history[:-1]) if h["role"] == "user"]
|
| 48 |
+
return chat_history, history
|
| 49 |
+
|
| 50 |
+
# Set up Gradio ChatInterface
|
| 51 |
+
with gr.Blocks() as demo:
|
| 52 |
+
chatbot = gr.Chatbot()
|
| 53 |
+
state = gr.State([]) # for storing history
|
| 54 |
+
txt = gr.Textbox(show_label=False, placeholder="Type your message...")
|
| 55 |
+
|
| 56 |
+
def user_submit(user_message, history):
|
| 57 |
+
return chat(user_message, history)
|
| 58 |
+
|
| 59 |
+
txt.submit(user_submit, [txt, state], [chatbot, state])
|
| 60 |
+
|
| 61 |
+
demo.launch()
|
|
|
|
|
|
|
|
|