Spaces:
Running
Running
initial commit
Browse files- app.py +64 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from llama_cpp import Llama
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
|
6 |
+
# Hugging FaceのAPIトークンを設定
|
7 |
+
os.environ["HUGGINGFACE_TOKEN"] = os.getenv("HUGGINGFACE_TOKEN")
|
8 |
+
|
9 |
+
model_name_or_path = "mmnga/ELYZA-japanese-Llama-2-7b-fast-instruct-gguf"
|
10 |
+
model_basename = "ELYZA-japanese-Llama-2-7b-fast-instruct-q5_K_M.gguf"
|
11 |
+
|
12 |
+
model_path = hf_hub_download(repo_id=model_name_or_path, filename=model_basename, revision="main")
|
13 |
+
llama = Llama(model_path)
|
14 |
+
|
15 |
+
def predict(messages):
|
16 |
+
# Llamaでの回答を取得(ストリーミングオン)
|
17 |
+
streamer = llama.create_chat_completion(messages, stream=True)
|
18 |
+
|
19 |
+
partial_message = ""
|
20 |
+
for msg in streamer:
|
21 |
+
message = msg['choices'][0]['delta']
|
22 |
+
if 'content' in message:
|
23 |
+
partial_message += message['content']
|
24 |
+
yield partial_message
|
25 |
+
|
26 |
+
|
27 |
+
def main():
|
28 |
+
st.title("Chat with ChatGPT Clone!")
|
29 |
+
|
30 |
+
# Session state for retaining messages
|
31 |
+
if 'messages' not in st.session_state:
|
32 |
+
st.session_state.messages = []
|
33 |
+
|
34 |
+
# Display chat messages from history on app rerun
|
35 |
+
for message in st.session_state.messages:
|
36 |
+
with st.chat_message(message["role"]):
|
37 |
+
st.markdown(f"{message['content']}")
|
38 |
+
|
39 |
+
# Input for the user message
|
40 |
+
user_message = st.chat_input("Your Message")
|
41 |
+
|
42 |
+
# React to user input
|
43 |
+
if user_message:
|
44 |
+
# Display user message in chat message container
|
45 |
+
with st.chat_message("user"):
|
46 |
+
st.markdown(f"{user_message}")
|
47 |
+
# Add user message to chat history
|
48 |
+
st.session_state.messages.append({"role": "user", "content": user_message})
|
49 |
+
|
50 |
+
with st.chat_message("assistant"):
|
51 |
+
message_placeholder = st.empty()
|
52 |
+
full_response = ""
|
53 |
+
|
54 |
+
for char in predict([{"role": m["role"], "content": m["content"]} for m in st.session_state.messages]):
|
55 |
+
full_response = char #+= char
|
56 |
+
message_placeholder.markdown(full_response + " ❚ ")
|
57 |
+
|
58 |
+
message_placeholder.markdown(full_response)
|
59 |
+
|
60 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
61 |
+
|
62 |
+
|
63 |
+
if __name__ == "__main__":
|
64 |
+
main()
|
requirements.txt
ADDED
Binary file (2 kB). View file
|
|