Skip to content

Commit c094de7

Browse files
committed
Add readme
1 parent 3d8d308 commit c094de7

File tree

8 files changed

+306
-225
lines changed

8 files changed

+306
-225
lines changed

.gitignore

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22
**/__pycache__/
33

44
# pytest cache
5-
**/.pytest_cache/
5+
**/.pytest_cache/
6+
7+
# Exclude .coverage files
8+
**/.coverage
9+
10+
# Exclude .vscode files
11+
.vscode/

README.md

+260-81
Large diffs are not rendered by default.

backend/api/main.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
2-
##################### FastAPI + TinyLlama + Docker #########################################
3-
Autor: Adrián Baeza Prieto
2+
##################### FastAPI + TinyLlama Backend API #########################################
3+
Author: Adrián Baeza Prieto
44
Github: @adribaeza
55
Python 3.10+
66
'''
@@ -74,7 +74,6 @@ async def get_current_user(token: str = Depends(oauth2_scheme)):
7474
# Load the model with the TinyLlama model
7575
pipe = pipeline("text-generation", model=LLM_MODEL, torch_dtype=torch.bfloat16, device_map="auto")
7676

77-
7877
class Message(BaseModel):
7978
role: str
8079
content: str
@@ -158,9 +157,4 @@ async def chat(request: ChatRequest, user: dict = Depends(get_current_user)):
158157
raise HTTPException(status_code=500, detail="Internal Server Error")
159158

160159
# Include main router in the API
161-
api.include_router(api_router)
162-
163-
# Execute the API with Uvicorn only if the script is executed directly in the local environment
164-
#if __name__ == '__main__':
165-
# import uvicorn
166-
# uvicorn.run(api)
160+
api.include_router(api_router)

backend/k8s/service.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ spec:
99
- protocol: TCP
1010
port: 8000
1111
targetPort: 8000
12-
nodePort: 30000 # Choose between 30000-32767
13-
type: NodePort
12+
type: LoadBalancer

backend/tests/test_main.py

+7-21
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
1+
'''
2+
##################### Backend Unit Testing #########################################
3+
Author: Adrián Baeza Prieto
4+
Github: @adribaeza
5+
Python 3.10+
6+
'''
17
import logging
28
from fastapi import HTTPException
39
import pytest
4-
#from fastapi.testclient import TestClient
5-
#need import api and static token from main.py, need upload main.py to the test folder
6-
from backend.api.main import api, STATIC_TOKEN, verify_token
10+
from backend.api.main import STATIC_TOKEN, verify_token
711

8-
# Configurar logging
912
logging.basicConfig(level=logging.DEBUG)
10-
# Configurar el cliente de prueba
11-
#client = TestClient(api)
1213

13-
#def test_verify_token():
14-
# logging.debug("Init test_verify_token")
15-
# # Test with valid token
16-
# logging.debug("Test with valid token")
17-
# response = client.post("/api/v1/chat", headers={"Authorization": f"Bearer {STATIC_TOKEN}"}, json={"text": "Hello"})
18-
# assert response.status_code == 200
19-
20-
# Test with invalid token
21-
# logging.debug("Test with invalid token")
22-
# response = client.post("/api/v1/chat", headers={"Authorization": "Bearer invalidtoken"}, json={"text": "Hello"})
23-
# assert response.status_code == 401
24-
# assert response.json() == {"detail": "Invalid authentication credentials"}
25-
26-
27-
#create test to check function verify_token in main.py without client
2814
def test_verify_token():
2915
logging.debug("Test with valid token")
3016
valid_token = STATIC_TOKEN

frontend/Readme.md

-87
This file was deleted.

frontend/app/main.py

+22-23
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
'''
2+
##################### Streamlit Chat With LLM Model #########################################
3+
Author: Adrián Baeza Prieto
4+
Github: @adribaeza
5+
Python 3.10+
6+
'''
17
import streamlit as st
28
import requests, logging, os
39
from dotenv import load_dotenv
@@ -20,30 +26,29 @@
2026
DEFAULT_TOP_K = 50
2127
DEFAULT_TOP_P = 0.9
2228

23-
24-
# Función para limpiar el historial de mensajes
29+
# Function to clear the chat history
2530
def clear_chat():
2631
st.session_state.messages = []
2732

2833
def main():
2934

30-
# Configuración de la página
35+
# Page configuration
3136
st.set_page_config(
3237
page_title="Chat with TinyLlama",
33-
page_icon=":robot_face:", # Puedes usar un emoji o una URL a un favicon específico
38+
page_icon=":robot_face:",
3439
layout="centered",
3540
initial_sidebar_state="auto",
3641
)
3742

38-
# Configuración de la interfaz
43+
# Interface title
3944
st.title("Chat with TinnyLLama LLM model")
4045
st.write("Simple chat interface to interact with TinyLlama LLM model")
4146

42-
# Añadir un botón para iniciar un nuevo chat
47+
# Add a button to clear the chat history
4348
if st.button("➕ New Chat", help="Click to start a new chat and clear the current conversation history"):
4449
clear_chat()
4550

46-
# Additional params with help text
51+
# Additional params with help text to adjust the LLM model behavior
4752
with st.expander("Config params", expanded=False):
4853
max_new_tokens = st.number_input(
4954
"Max New Tokens",
@@ -75,28 +80,28 @@ def main():
7580
help="The cumulative probability of parameter highest probability vocabulary tokens to keep for nucleus sampling."
7681
)
7782

78-
83+
# Check if the session state has the messages attribute to initialize it
7984
if "messages" not in st.session_state:
8085
st.session_state.messages = []
8186

87+
# Iterate over the messages in the session state to display them in the chat
8288
for message in st.session_state.messages:
8389
with st.chat_message(message["role"]):
8490
st.markdown(message["content"])
8591

86-
92+
# Add a chat input to interact with the assistant
8793
if prompt := st.chat_input("What is up?"):
94+
95+
# Add the user message to the chat history
8896
st.session_state.messages.append({"role": "user", "content": prompt})
8997
with st.chat_message("user"):
9098
st.markdown(prompt)
91-
9299
headers = {
93100
"Authorization": f"Bearer {STATIC_TOKEN}",
94101
"Content-Type": "application/json"
95102
}
96-
# Construir el historial de la conversación
103+
# Build the data payload for the API request
97104
conversation_history = [{"role": msg["role"], "content": msg["content"]} for msg in st.session_state.messages]
98-
99-
100105
data = {
101106
"messages": conversation_history,
102107
"max_new_tokens": max_new_tokens,
@@ -106,6 +111,8 @@ def main():
106111
"top_p": top_p
107112
}
108113
logging.info(f"Request data: {data}")
114+
115+
# Make a request to the API
109116
try:
110117
with st.spinner("The assistant is thinking..."):
111118
response = requests.post("http://host.docker.internal:8000/api/v1/chat", headers=headers, json=data)
@@ -123,7 +130,7 @@ def main():
123130
st.error("Failed to connect to the API")
124131
logging.error(f"Failed to connect to the API: {e}")
125132

126-
# Añadir un footer con el texto deseado
133+
# Add a footer with the app information
127134
st.markdown(
128135
"""
129136
<style>
@@ -145,15 +152,7 @@ def main():
145152
unsafe_allow_html=True
146153
)
147154

155+
# Run the main function
148156
if __name__ == "__main__":
149157
main()
150-
151-
#'''
152-
#### Run the Streamlit app
153-
#To run the Streamlit app, execute the following command in the terminal:
154-
#
155-
# ```bash
156-
# streamlit run frontend/app/main.py
157-
# ```
158-
#'''
159158

frontend/tests/test_main.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
# test_main.py
1+
'''
2+
##################### Frontend Unit Testing #########################################
3+
Author: Adrián Baeza Prieto
4+
Github: @adribaeza
5+
Python 3.10+
6+
'''
27
import unittest
38
from unittest.mock import patch, MagicMock
49
from frontend.app.main import clear_chat

0 commit comments

Comments
 (0)