akhilgautam commited on
Commit
a1d39bd
·
unverified ·
1 Parent(s): f6eb9de

Add files via upload

Browse files

added code from hugging face to github

Files changed (6) hide show
  1. Dockerfile +25 -0
  2. README.md +10 -0
  3. app.py +15 -0
  4. main.py +61 -0
  5. model_utils.py +42 -0
  6. requirements.txt +11 -0
Dockerfile ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ # Install system dependencies
4
+ RUN apt-get update && apt-get install -y \
5
+ build-essential \
6
+ software-properties-common \
7
+ && rm -rf /var/lib/apt/lists/*
8
+
9
+ # Set the working directory
10
+ WORKDIR /code
11
+
12
+ RUN mkdir /.cache && chmod 777 /.cache
13
+
14
+ # Copy the requirements file
15
+ COPY ./requirements.txt /code/requirements.txt
16
+ COPY ./weights-roberta-base.h5 /code/weights-roberta-base.h5
17
+
18
+ # Install Python dependencies
19
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
20
+
21
+ # Copy the application code
22
+ COPY . /code/
23
+
24
+ # Set the command to run the FastAPI application
25
+ CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Personality Assesment
3
+ emoji: 🏢
4
+ colorFrom: blue
5
+ colorTo: indigo
6
+ sdk: docker
7
+ pinned: false
8
+ ---
9
+
10
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from model_utils import predict_personality
3
+
4
+ # Create Gradio interface
5
+ iface = gr.Interface(
6
+ fn=predict_personality,
7
+ inputs=gr.Textbox(lines=5, label="Enter text for personality prediction"),
8
+ outputs=gr.Label(num_top_classes=5, label="Personality Traits"),
9
+ title="Personality Prediction with RoBERTa",
10
+ description="Enter some text to predict personality traits using a fine-tuned RoBERTa model."
11
+ )
12
+
13
+ # Launch the interface
14
+ if __name__ == "__main__":
15
+ iface.launch()
main.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # API/main.py
2
+
3
+ # main.py (in the root directory)
4
+
5
+ import sys
6
+ import os
7
+ from model_utils import predict_personality
8
+ from fastapi import FastAPI
9
+
10
+ app = FastAPI()
11
+
12
+ @app.get("/")
13
+ async def root():
14
+ return {"message": "Personality Assessment API is running"}
15
+
16
+ @app.get("/predict")
17
+ async def predict_personality_get(text: str):
18
+ try:
19
+ predictions = predict_personality(text)
20
+ return {"predictions": predictions}
21
+ except NameError:
22
+ return {"error": "predict_personality function not available"}
23
+
24
+
25
+ if __name__ == "__main__":
26
+ import uvicorn
27
+ uvicorn.run(app, host="0.0.0.0", port=8000)
28
+
29
+
30
+
31
+
32
+ """ from fastapi import FastAPI, Request
33
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
34
+ import torch
35
+
36
+ app = FastAPI()
37
+
38
+ # Load the model and tokenizer
39
+ tokenizer = AutoTokenizer.from_pretrained("Minej/bert-base-personality")
40
+ model = AutoModelForSequenceClassification.from_pretrained("Minej/bert-base-personality")
41
+
42
+ # Define the personality trait labels
43
+ labels = ["Extroversion", "Neuroticism", "Agreeableness", "Conscientiousness", "Openness"]
44
+
45
+ # Function to predict personality traits
46
+ def predict_personality(text):
47
+ inputs = tokenizer(text, return_tensors="pt")
48
+ outputs = model(**inputs)[0]
49
+ probabilities = torch.softmax(outputs, dim=1)
50
+ predictions = [{"trait": label, "score": float(prob)} for label, prob in zip(labels, probabilities[0])]
51
+ return predictions
52
+
53
+ # Root path handler
54
+ @app.get("/")
55
+ async def root():
56
+ return {"message": "Personality Assessment API is running"}
57
+
58
+ @app.get("/predict")
59
+ async def predict_personality_get(text: str):
60
+ predictions = predict_personality(text)
61
+ return {"predictions": predictions} """
model_utils.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # model_utils.py
2
+
3
+ import os
4
+ import tensorflow as tf
5
+ from transformers import AutoTokenizer, TFAutoModelForSequenceClassification
6
+
7
+ # Define the personality trait labels
8
+ traits = ['cAGR', 'cCON', 'cEXT', 'cOPN', 'cNEU']
9
+
10
+ def load_model_and_weights():
11
+ model_name = "roberta-base"
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+ model = TFAutoModelForSequenceClassification.from_pretrained(
14
+ model_name,
15
+ num_labels=len(traits),
16
+ problem_type="multi_label_classification"
17
+ )
18
+
19
+ # Load custom weights
20
+ weights_path = os.path.join(os.getcwd(), 'weights-roberta-base.h5')
21
+ if os.path.exists(weights_path):
22
+ try:
23
+ model.load_weights(weights_path)
24
+ print("Custom weights loaded successfully.")
25
+ except Exception as e:
26
+ print(f"Error loading weights: {str(e)}")
27
+ print("Using default weights.")
28
+ else:
29
+ print(f"Warning: Custom weights file not found at {weights_path}")
30
+ print("Using default weights.")
31
+
32
+ return tokenizer, model
33
+
34
+ # Load the model and tokenizer
35
+ tokenizer, model = load_model_and_weights()
36
+
37
+ def predict_personality(text):
38
+ inputs = tokenizer(text, return_tensors="tf", truncation=True, padding=True, max_length=512)
39
+ outputs = model(inputs)
40
+ probabilities = tf.nn.sigmoid(outputs.logits)[0] # Using sigmoid for multi-label
41
+ predictions = [{"trait": trait, "score": float(prob)} for trait, prob in zip(traits, probabilities)]
42
+ return predictions
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ fastapi==0.92.0
2
+ uvicorn==0.20.0
3
+ transformers==4.27.4
4
+ torch==1.13.1
5
+ gradio==3.23.0
6
+ numpy==1.21.0
7
+ pandas==1.3.0
8
+ scikit-learn==0.24.2
9
+ wget
10
+ requests
11
+ tensorflow