Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fakhriddin ai #2

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 42 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,45 @@ Welcome to the Ruby + AI Hacking Fest! This online hackathon is a weekend-long e

# Rules and Guidelines
### Setup
* The application must run on a local machine (Mac or Linux).
* Integrate any existing and publicly available AI model with a Rails application.
* Docker is preferable but not mandatory.

### Development Rules
* You are allowed to use Hugging Face and their open-source libraries.
* All work must be original and created during the hackathon.
* Usage of pre-built integrations for Ruby and Rails, like Ollama, is prohibited.

### Submission Requirements
* A public repository on GitHub containing the application source code.
* The AI model and integration details.
* Clear instructions on how to set up and run the application locally.

# Judging Criteria
### Functionality:
* How well does the application run?
* Does it integrate the AI model effectively?

### Innovation:
* How unique and creative is the solution?
* Technical Difficulty:
* What challenges did you overcome?
* What was utilised from your previous experience?

### Documentation:
* Are the setup instructions clear and easy to follow?
* Was it clear to follow and get it the app up and running?

### Presentation:
* How well is it presented at the demo session?
* How interesting the idea and its applicability?

# Prizes
* Grand Prize: $500 for the best overall project.
It is sentiment analysis of the message. Comments belonging to the post can be **NEGATIVE**, **NEUTRAL** and **POSITIVE**. It is a microservice architecture, Flask, pytorch, transformers are used for AI part (labelling) and Rails is for web. This app can be used to find out what opinions people have about the news or something new. This app can be very helpful in areas such as legislation, commersing, business.

**AI**
transformers library is used from HuggingFace, pipeline analyzes loaded text

### Python
1. Create a new virtual environment:
```python
python3 -m venv venv
```
2. Activate the virtual environment:
```python
source venv/bin/activate
```
3. Install Flask and transformers dependencies:
```python
pip install flask transformers
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
```


### Ruby on Rails Setup

1. Database setup:
```ruby
rails db:create
rails db:migrate
rails db:seed
```

### Running the application
1. Start the Flask server:
```python
python3 save_model.py
python3 app.py
```
2. Start the Ruby on Rails server:
```ruby
rails s
```

![alt text](image.png)
17 changes: 17 additions & 0 deletions flask/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install the necessary packages
RUN pip install --no-cache-dir flask transformers

# Make port 5000 available to the world outside this container
EXPOSE 5000

# Run app.py when the container launches
CMD ["python", "app.py"]
24 changes: 24 additions & 0 deletions flask/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import torch
from flask import Flask, request, jsonify
from transformers import pipeline

app = Flask(__name__)

model_path = "./cache1/model/cardiffnlp/twitter-roberta-base-sentiment-latest"
tokenizer_path = "./cache1/tokenizer/cardiffnlp/twitter-roberta-base-sentiment-latest"

# Load the Hugging Face model
classifier = pipeline("sentiment-analysis", model=model_path, tokenizer=tokenizer_path)

@app.route('/predict', methods=['POST'])
def predict():
data = request.json
text = data.get("text")
if not text:
return jsonify({"error": "No text provided"}), 400

result = classifier(text)
return jsonify(result)

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
13 changes: 13 additions & 0 deletions flask/save_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import torch
from transformers import AutoModelForSequenceClassification, AutoTokenizer

# model name
model_name = "cardiffnlp/twitter-roberta-base-sentiment-latest"

# download tokenizer & save
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.save_pretrained(f"cache1/tokenizer/{model_name}")

# download model & save
model = AutoModelForSequenceClassification.from_pretrained(model_name)
model.save_pretrained(f"cache1/model/{model_name}")
Binary file added image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions rails-app/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# See https://git-scm.com/docs/gitattributes for more about git attribute files.

# Mark the database schema as having been generated.
db/schema.rb linguist-generated

# Mark any vendored files as having been vendored.
vendor/* linguist-vendored
39 changes: 39 additions & 0 deletions rails-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore pidfiles, but keep the directory.
/tmp/pids/*
!/tmp/pids/
!/tmp/pids/.keep

# Ignore uploaded files in development.
/storage/*
!/storage/.keep
/tmp/storage/*
!/tmp/storage/
!/tmp/storage/.keep

/public/assets

# Ignore master key for decrypting credentials and more.
/config/master.key

/app/assets/builds/*
!/app/assets/builds/.keep

/node_modules

# Ignore application configuration
/config/application.yml
1 change: 1 addition & 0 deletions rails-app/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.1.4
80 changes: 80 additions & 0 deletions rails-app/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
source "https://rubygems.org"
git_source(:github) { |repo| "/~https://github.com/#{repo}.git" }

ruby "3.1.4"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.8", ">= 7.0.8.3"

# The original asset pipeline for Rails [/~https://github.com/rails/sprockets-rails]
gem "sprockets-rails"

# Use postgresql as the database for Active Record
gem "pg", "~> 1.1"

# Use the Puma web server [/~https://github.com/puma/puma]
gem "puma", "~> 5.0"
gem "figaro"
gem "pry"
gem "devise"
gem "simple_form"
gem "rest-client"
gem "pundit"
# Bundle and transpile JavaScript [/~https://github.com/rails/jsbundling-rails]
gem "jsbundling-rails"

# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"

# Bundle and process CSS [/~https://github.com/rails/cssbundling-rails]
gem "cssbundling-rails"

# Build JSON APIs with ease [/~https://github.com/rails/jbuilder]
gem "jbuilder"

# Use Redis adapter to run Action Cable in production
gem "redis", "~> 4.0"

# Use Kredis to get higher-level data types in Redis [/~https://github.com/rails/kredis]
# gem "kredis"

# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false

# Use Sass to process CSS
# gem "sassc-rails"

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
end

group :development do
# Use console on exceptions pages [/~https://github.com/rails/web-console]
gem "web-console"

# Add speed badges [/~https://github.com/MiniProfiler/rack-mini-profiler]
# gem "rack-mini-profiler"

# Speed up commands on slow machines / big apps [/~https://github.com/rails/spring]
# gem "spring"
end

group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem "capybara"
gem "selenium-webdriver"

end
Loading