STONKS APP 📈📉- Stocks helper for Beginners
Tags: cloudflarechallenge, devchallenge, ai, serverless
Published: 2024-04-14 15:26:12

*This is a submission for the [Cloudflare AI Challenge](https://dev.to/devteam/join-us-for-the-cloudflare-ai-challenge-3000-in-prizes-5f99).*

## What I Built

- Stonks is a beginner-friendly stock information app that provides an intuitive and user-friendly platform for accessing stock data and insights.
- It is a web-based application built on Streamlit, leveraging the power of serverless Cloudflare Workers and Workers AI to generate stock information from the polygon.io API.

## Demo

### [STONKS APP 📈📉](https://stonks-stonks.streamlit.app)

### [Stonks app backend](https://stonks-worker.rony000013.workers.dev/)

{%embed https://youtu.be/QZX2xStPvNU %}

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vv8aaptb6vwpuronj56q.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asuamwazt3ouqvirsaj5.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l49uq4d4x3edkg0gteo5.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gbnvmiql6yzelmrkvfl1.png)

## My Code

The code for the Stonks App is available on GitHub:

- ### Streamlit app  <https://github.com/rony0000013/stonks>

    {%embed <https://github.com/rony0000013/stonks> %}

- ### Cloudflare Worker  <https://github.com/rony0000013/stonks-worker>

    {%embed <https://github.com/rony0000013/stonks-worker> %}

### Tech Stack -

1. **Streamlit** - to build and deploy the app
2. **fuzzywuzzy** - to search voice text to the ticker data
3. **Pytickersymbols** - to get all stock tickers
4. **Plotly** - do display the stock candlestick chart
5. **Poetry** - to manage libraries and project
6. **Wrangler** - to manage and deploy the worker
7. **hono** - backend server to handle requests to the worker
8. **cloudflare workers ai** - to run the ai models serverlessly
9. **cloudflare kv** - to cache the worker requests
10. **cloudflare workers** - to deploy and run the server

The repository includes a README file with installation instructions, as well as a LICENSE file for the project.

## Journey

- The development of the Stonks App was an exciting and challenging journey. I utilized various technologies and models to create this beginner-friendly stock information platform.

- I was planning on building a easy way to view information about stocks since huge percent of indians are not stock investors. so th opportunity to build such an app with cloudflare was very intresting. But the only thing was that i saw that on 12/04/2024 with only 2 more days remaining !!

- I started with the backend/worker. the start was pretty easy to my surprise thanks to the video from cloudflare [Youtube Video 📺](https://www.youtube.com/watch?v=H7Qe96fqg1M). Since my first time using Cloudflare Workers, Hono and Typecript (relatively more). So the start was easy.

- I really liked the Wrangler CLI very intuitive to use. The workers AI models used pretty well and documentation was plenty. The Ploygon API also has clean docs with easy to use API. Cloudflare KV was very easy to implement the caching mechanism. I was not able to implement the swagger ui like the one in fastapi.

- For the frontend It was my first time to use streamlit to build a website size app with complex logics. It was more or less okay.

- The main trail to problems arised when I joined frontend and backend. both had to be changed several times to pass data correctly to each other and also the caching was implemented in multiple stages like in api routes, api calling, streamlit functions which ultimately prevented the potential problem of too many requests to use up my api limits.

- I had an idea of generating the stock candlestick graph and stylize it with the image generation models. First problem was getiing the chart image. After searching various libraries

- I came to a realization that was cloudflare workers is a serverless environment usual javascript libraries which call with DOM to generate charts did not work. After trying multiple times I ultimately decided that python's Plotly will do the chart creation.

- Second problem arose when I tried to modify image using workers ai models. Here I realized that most except `stable-diffusion-v1-5-img2img` was not able to take input an image despite given in the docs input schema. The output of the only model which was able to stylize was very poor. So, I gave up on this Idea.

- Rest all models were easy to implement in the app and I ultimately was able to finish this in 2 days.

- Looking ahead, I plan to improve the Stonks App by adding new features and exploring additional ways to leverage Workers AI. Some potential enhancements include implementing advanced stock analysis models, implementing a more fun and easy way to show stock charts.

## Multiple Models and/or Triple Task Types

The Stonks App utilized multiple models from Workers AI to generate stock information, news analysis, and sentiment analysis.

- Generating the stock information and chatting with stock data was done by

    ```json
    [
        "@hf/thebloke/zephyr-7b-beta-awq",
        "@cf/qwen/qwen1.5-0.5b-chat",
        "@hf/nexusflow/starling-lm-7b-beta",
        "@hf/thebloke/llamaguard-7b-awq",
        "@hf/thebloke/neural-chat-7b-v3-1-awq",
        "@cf/meta/llama-2-7b-chat-fp16",
        "@cf/mistral/mistral-7b-instruct-v0.1",
        "@cf/tinyllama/tinyllama-1.1b-chat-v1.0",
        "@hf/mistral/mistral-7b-instruct-v0.2",
        "@hf/thebloke/codellama-7b-instruct-awq",
        "@hf/mistralai/mistral-7b-instruct-v0.2",
        "@cf/thebloke/discolm-german-7b-v1-awq",
        "@cf/meta/llama-2-7b-chat-int8",
        "@hf/thebloke/mistral-7b-instruct-v0.1-awq",
        "@hf/thebloke/openchat_3.5-awq",
        "@cf/qwen/qwen1.5-7b-chat-awq",
        "@hf/thebloke/llama-2-13b-chat-awq",
        "@hf/thebloke/openhermes-2.5-mistral-7b-awq",
        "@cf/tiiuae/falcon-7b-instruct",
        "@hf/nousresearch/hermes-2-pro-mistral-7b",
        "@cf/qwen/qwen1.5-1.8b-chat",
        "@cf/microsoft/phi-2",
        "@hf/google/gemma-7b-it",
        "@cf/qwen/qwen1.5-14b-chat-awq",
        "@cf/openchat/openchat-3.5-0106",
        "@cf/google/gemma-2b-it-lora",
        "@cf/google/gemma-7b-it-lora",
    ]
    ```

- Summarizing the generated output was done by `@cf/facebook/bart-large-cnn`

- Translating the output was done by `@cf/meta/m2m100-1.2b`

- Generating Sentiments from the news articles was done by `@cf/huggingface/distilbert-sst-2-int8`

- Converting the voice input to text for ticker search was done by `@cf/openai/whisper`