Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
import torch
|
| 5 |
+
from transformers import pipeline
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
quantization_config = BitsAndBytesConfig(load_in_4bit=True)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
model_name = "masakhane/zephyr-7b-gemma-sft-african-alpaca"
|
| 13 |
+
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 15 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, quantization_config=quantization_config)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
pipe = pipeline("text-generation", model=model,tokenizer=tokenizer, torch_dtype=torch.bfloat16, device_map="auto")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
if 'messages' not in st.session_state:
|
| 23 |
+
st.session_state.messages = [
|
| 24 |
+
{
|
| 25 |
+
"role": "system",
|
| 26 |
+
"content": "You are a friendly chatbot who answewrs question in given language",
|
| 27 |
+
},
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
def ask_model(question):
|
| 31 |
+
# Placeholder for model interaction logic
|
| 32 |
+
# You would replace this with actual code to query the model
|
| 33 |
+
st.session_state.messages.append({"role": "user", "content": f"{question}"})
|
| 34 |
+
|
| 35 |
+
prompt = pipe.tokenizer.apply_chat_template(st.session_state.messages, tokenize=False, add_generation_prompt=True)
|
| 36 |
+
outputs = pipe(prompt, max_new_tokens=1000, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
|
| 37 |
+
print(outputs[0]["generated_text"].split("<|assistant|>")[-1])
|
| 38 |
+
|
| 39 |
+
st.session_state.messages.append({"role": "assistant", "content": f"{outputs[0]['generated_text'].split('<|assistant|>')[-1]}"})
|
| 40 |
+
return st.session_state.messages
|
| 41 |
+
|
| 42 |
+
st.title('LLM Interaction Interface')
|
| 43 |
+
|
| 44 |
+
user_input = st.text_input("Ask a question:")
|
| 45 |
+
|
| 46 |
+
if user_input:
|
| 47 |
+
# This function is supposed to send the question to the LLM and get the response
|
| 48 |
+
response = ask_model(user_input)
|
| 49 |
+
st.text_area("Response:", value=response[-1]['content'], height=300, max_chars=None, help=None)
|
| 50 |
+
st.json({'value':response},expanded=False)
|