Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +73 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from llama_index.core.llms import ChatMessage
|
| 3 |
+
import logging
|
| 4 |
+
import time
|
| 5 |
+
from llama_index.llms.ollama import Ollama
|
| 6 |
+
|
| 7 |
+
# Set up logging
|
| 8 |
+
logging.basicConfig(level=logging.INFO)
|
| 9 |
+
|
| 10 |
+
# Initialize chat history in session state
|
| 11 |
+
if 'messages' not in st.session_state:
|
| 12 |
+
st.session_state.messages = []
|
| 13 |
+
|
| 14 |
+
# Function to stream chat response based on selected model
|
| 15 |
+
def stream_chat(model, messages):
|
| 16 |
+
# Integrate with actual LLM streaming method
|
| 17 |
+
try:
|
| 18 |
+
llm = Ollama(model=model, request_timeout=120.0)
|
| 19 |
+
resp = llm.stream_chat(messages)
|
| 20 |
+
response = ""
|
| 21 |
+
response_placeholder = st.empty()
|
| 22 |
+
for r in resp:
|
| 23 |
+
response += r.delta
|
| 24 |
+
response_placeholder.write(response)
|
| 25 |
+
logging.info(f"Model: {model}, Messages: {messages}, Response: {response}")
|
| 26 |
+
return response
|
| 27 |
+
except Exception as e:
|
| 28 |
+
logging.error(f"Error during streaming: {str(e)}")
|
| 29 |
+
raise e
|
| 30 |
+
|
| 31 |
+
# Streamlit app setup
|
| 32 |
+
def main():
|
| 33 |
+
st.title("Chat with LLMs Models")
|
| 34 |
+
logging.info("App started")
|
| 35 |
+
|
| 36 |
+
# Sidebar for model selection
|
| 37 |
+
model = st.sidebar.selectbox("Choose a model", ["llama3", "phi3", "mistral"])
|
| 38 |
+
logging.info(f"Model selected: {model}")
|
| 39 |
+
|
| 40 |
+
# Prompt for user input and save to chat history
|
| 41 |
+
if prompt := st.chat_input("Votre question"):
|
| 42 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 43 |
+
logging.info(f"User input: {prompt}")
|
| 44 |
+
|
| 45 |
+
# Immediately display the user's query
|
| 46 |
+
for message in st.session_state.messages:
|
| 47 |
+
with st.chat_message(message["role"]):
|
| 48 |
+
st.write(message["content"])
|
| 49 |
+
|
| 50 |
+
# If last message is not from assistant, generate a new response
|
| 51 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
| 52 |
+
with st.chat_message("assistant"):
|
| 53 |
+
# Start timing
|
| 54 |
+
start_time = time.time()
|
| 55 |
+
logging.info("Generating response")
|
| 56 |
+
# Generate the bot's response with error handling for missing files
|
| 57 |
+
with st.spinner("En train d'écrire..."):
|
| 58 |
+
try:
|
| 59 |
+
messages = [ChatMessage(role=msg["role"], content=msg["content"]) for msg in st.session_state.messages]
|
| 60 |
+
response_message = stream_chat(model, messages)
|
| 61 |
+
# Calculate the duration
|
| 62 |
+
duration = time.time() - start_time
|
| 63 |
+
response_message_with_duration = f"{response_message}\n\nDuration: {duration:.2f} seconds"
|
| 64 |
+
st.session_state.messages.append({"role": "assistant", "content": response_message_with_duration})
|
| 65 |
+
st.write(f"Duration: {duration:.2f} seconds")
|
| 66 |
+
logging.info(f"Response: {response_message}, Duration: {duration:.2f} s")
|
| 67 |
+
except Exception as e:
|
| 68 |
+
st.session_state.messages.append({"role": "assistant", "content": str(e)})
|
| 69 |
+
st.error("Une erreur s'est produite lors de la génération de la réponse.")
|
| 70 |
+
logging.error(f"Error: {str(e)}")
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
llama-index
|
| 3 |
+
ollama
|