Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,117 +1,79 @@
|
|
1 |
-
#
|
2 |
-
|
3 |
-
# from firebase_admin import firestore
|
4 |
-
import io
|
5 |
-
from fastapi import FastAPI, File, UploadFile
|
6 |
-
from werkzeug.utils import secure_filename
|
7 |
-
# import speech_recognition as sr
|
8 |
-
import subprocess
|
9 |
-
import os
|
10 |
-
import requests
|
11 |
-
import random
|
12 |
-
import pandas as pd
|
13 |
-
from pydub import AudioSegment
|
14 |
-
from datetime import datetime
|
15 |
-
from datetime import date
|
16 |
-
import numpy as np
|
17 |
-
# from sklearn.ensemble import RandomForestRegressor
|
18 |
-
import shutil
|
19 |
-
import json
|
20 |
-
# from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
|
21 |
-
from pydantic import BaseModel
|
22 |
-
from typing import Annotated
|
23 |
-
# from transformers import BertTokenizerFast, EncoderDecoderModel
|
24 |
-
import torch
|
25 |
-
import re
|
26 |
-
# from transformers import AutoTokenizer, T5ForConditionalGeneration
|
27 |
-
from fastapi import Form
|
28 |
-
# from transformers import AutoModelForSequenceClassification
|
29 |
-
# from transformers import TFAutoModelForSequenceClassification
|
30 |
-
# from transformers import AutoTokenizer, AutoConfig
|
31 |
-
import numpy as np
|
32 |
-
# from scipy.special import softmax
|
33 |
-
from sentence_transformers import SentenceTransformer
|
34 |
|
|
|
|
|
35 |
|
|
|
|
|
36 |
|
|
|
|
|
37 |
|
38 |
-
#
|
39 |
-
|
40 |
|
|
|
|
|
41 |
|
|
|
|
|
42 |
|
|
|
|
|
|
|
43 |
|
|
|
|
|
|
|
|
|
44 |
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
47 |
|
48 |
-
|
|
|
|
|
|
|
49 |
|
|
|
|
|
|
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
from fastapi.middleware.cors import CORSMiddleware
|
54 |
-
from fastapi.responses import JSONResponse
|
55 |
|
|
|
|
|
56 |
|
57 |
-
#
|
|
|
|
|
58 |
|
|
|
59 |
|
60 |
-
#
|
61 |
-
#
|
62 |
-
# 'jpg', 'jpeg', 'gif', 'ogg', 'mp3', 'wav'}
|
63 |
|
|
|
|
|
64 |
|
65 |
-
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
CORSMiddleware,
|
69 |
-
allow_origins=['*'],
|
70 |
-
allow_credentials=True,
|
71 |
-
allow_methods=['*'],
|
72 |
-
allow_headers=['*'],
|
73 |
-
)
|
74 |
-
|
75 |
-
|
76 |
-
# cred = credentials.Certificate('key.json')
|
77 |
-
# app1 = firebase_admin.initialize_app(cred)
|
78 |
-
# db = firestore.client()
|
79 |
-
# data_frame = pd.read_csv('data.csv')
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
@app.on_event("startup")
|
84 |
-
async def startup_event():
|
85 |
-
print("on startup")
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
@app.post("/")
|
91 |
-
async def get_answer(q: Query ):
|
92 |
-
|
93 |
-
text = q.text
|
94 |
-
text_e = model.encode(text)
|
95 |
-
|
96 |
-
|
97 |
-
dict={ }
|
98 |
-
|
99 |
-
c=0
|
100 |
-
text_e= text_e.tolist()
|
101 |
-
|
102 |
-
for num in text_e:
|
103 |
-
dict[c]= num
|
104 |
-
c= c+1
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
return dict
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
return "hello"
|
115 |
-
|
116 |
|
|
|
|
|
|
|
|
|
|
|
117 |
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use the official Python 3.9 image as the base image
|
2 |
+
FROM python:3.9
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Expose the port
|
5 |
+
EXPOSE 7860
|
6 |
|
7 |
+
# Keeps Python from generating .pyc files in the container
|
8 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
9 |
|
10 |
+
# Turns off buffering for easier container logging
|
11 |
+
ENV PYTHONUNBUFFERED=1
|
12 |
|
13 |
+
# Set the PYNGROK_CONFIG environment variable
|
14 |
+
ENV PYNGROK_CONFIG /tmp/pyngrok.yml
|
15 |
|
16 |
+
# Set the NGROK_PATH environment variable to a writable location
|
17 |
+
ENV NGROK_PATH /tmp/ngrok
|
18 |
|
19 |
+
# Copy requirements.txt into the container
|
20 |
+
COPY requirements.txt .
|
21 |
|
22 |
+
# Upgrade pip and install the required packages
|
23 |
+
RUN pip install --upgrade pip && \
|
24 |
+
pip install -r requirements.txt
|
25 |
|
26 |
+
# Install sudo and create the necessary directories before copying the files
|
27 |
+
RUN apt-get update && \
|
28 |
+
apt-get install -y sudo && \
|
29 |
+
mkdir -p /code/image
|
30 |
|
31 |
+
# Creates a non-root user with an explicit UID and adds permission to access the /code folder
|
32 |
+
RUN adduser -u 5678 --disabled-password --gecos "" appuser && \
|
33 |
+
usermod -aG sudo appuser && \
|
34 |
+
usermod -aG root appuser && \
|
35 |
+
chown -R appuser:appuser /code
|
36 |
|
37 |
+
# Create the pyngrok bin directory and set the ownership and permissions for appuser
|
38 |
+
RUN mkdir -p /usr/local/lib/python3.9/site-packages/pyngrok/bin && \
|
39 |
+
chown -R appuser:appuser /usr/local/lib/python3.9/site-packages/pyngrok/bin && \
|
40 |
+
chmod -R 777 /usr/local/lib/python3.9/site-packages/pyngrok/bin
|
41 |
|
42 |
+
RUN mkdir -p /.ngrok2 && \
|
43 |
+
chown -R appuser:appuser /.ngrok2 && \
|
44 |
+
chmod -R 777 /.ngrok2
|
45 |
|
46 |
+
RUN apt-get update && \
|
47 |
+
apt-get install -y curl
|
|
|
|
|
48 |
|
49 |
+
# Set the working directory and copy the files
|
50 |
+
WORKDIR /code
|
51 |
|
52 |
+
# Set the ownership and permissions for the /code directory and its contents
|
53 |
+
RUN chown -R appuser:appuser /code && \
|
54 |
+
chmod -R 777 /code
|
55 |
|
56 |
+
COPY . /code
|
57 |
|
58 |
+
# RUN chown -R appuser:appuser /code/data.csv && \
|
59 |
+
# chmod -R 777 /code/data.csv
|
|
|
60 |
|
61 |
+
# Copy the pyngrok.yml configuration file
|
62 |
+
COPY pyngrok.yml /tmp/pyngrok.yml
|
63 |
|
64 |
+
# Set the TRANSFORMERS_CACHE environment variable to a cache directory inside /tmp
|
65 |
+
ENV TRANSFORMERS_CACHE /tmp/transformers_cache
|
66 |
+
ENV TORCH_HOME /tmp/torch_cache
|
67 |
|
68 |
+
USER appuser
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
# Start the application using pyngrok
|
71 |
+
# CMD python main.py
|
72 |
+
# Get the public IP address and display it
|
73 |
+
RUN curl -s https://api.ipify.org | xargs echo "Public IP:"
|
74 |
+
RUN pip install gunicorn
|
75 |
|
76 |
+
# Start the Uvicorn server
|
77 |
+
# ENTRYPOINT ["python", "main.py"]
|
78 |
+
# CMD ["sh", "-c", "python main.py & sleep infinity"]
|
79 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860","--workers","2"]
|