Spaces:
Running
Running
Create aap.py
Browse files
aap.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
import nest_asyncio
|
| 3 |
+
from pyngrok import ngrok
|
| 4 |
+
import uvicorn
|
| 5 |
+
import requests
|
| 6 |
+
import torch
|
| 7 |
+
# from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
|
| 8 |
+
|
| 9 |
+
# tokenizer = DistilBertTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
| 10 |
+
# model = DistilBertForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
|
| 11 |
+
|
| 12 |
+
app = FastAPI()
|
| 13 |
+
|
| 14 |
+
url = "https://sentiment-analysis9.p.rapidapi.com/sentiment"
|
| 15 |
+
#user_input = input("Please enter a text for sentiment analysis: ")
|
| 16 |
+
def call_sentiment_api(user_input):
|
| 17 |
+
payload = [
|
| 18 |
+
{
|
| 19 |
+
"id": "1",
|
| 20 |
+
"language": "en",
|
| 21 |
+
"text":user_input
|
| 22 |
+
}
|
| 23 |
+
]
|
| 24 |
+
headers = {
|
| 25 |
+
"content-type": "application/json",
|
| 26 |
+
"Accept": "application/json",
|
| 27 |
+
"X-RapidAPI-Key": "5cf8fcaf61msh613f010a34f3576p1953e5jsn110a1e6c667d",
|
| 28 |
+
"X-RapidAPI-Host": "sentiment-analysis9.p.rapidapi.com"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
response = requests.post(url, json=payload, headers=headers)
|
| 32 |
+
|
| 33 |
+
print(response.json())
|
| 34 |
+
return response.json()
|
| 35 |
+
|
| 36 |
+
# def sentiment_model_hf(user_input):
|
| 37 |
+
# inputs = tokenizer(user_input, return_tensors="pt")
|
| 38 |
+
# with torch.no_grad():
|
| 39 |
+
# logits = model(**inputs).logits
|
| 40 |
+
# predicted_class_id = logits.argmax().item()
|
| 41 |
+
|
| 42 |
+
# return model.config.id2label[predicted_class_id]
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
@app.get('/sentiment_ra/{user_input}')
|
| 46 |
+
async def sentiment(user_input):
|
| 47 |
+
return call_sentiment_api(user_input)
|
| 48 |
+
|
| 49 |
+
# @app.get('/sentiment_hf/{user_input}')
|
| 50 |
+
# async def sentiment_hf(user_input):
|
| 51 |
+
# return sentiment_model_hf(user_input)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
@app.get('/a')
|
| 55 |
+
async def abc():
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
return "Hello Atom Camp -- Our first Endpoint"
|
| 59 |
+
|
| 60 |
+
@app.get('/2nd')
|
| 61 |
+
async def atom():
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
return "its our 2nd endpoint"
|
| 65 |
+
|
| 66 |
+
@app.get('/{multiply}')
|
| 67 |
+
async def atom(multiply):
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
return multiply*10
|
| 71 |
+
|
| 72 |
+
@app.get('/')
|
| 73 |
+
async def html():
|
| 74 |
+
|
| 75 |
+
return "Welcome to Our FastAPI Endpoints"
|
| 76 |
+
|
| 77 |
+
ngrok_tunnel = ngrok.connect(8000)
|
| 78 |
+
print('Public URL:', ngrok_tunnel.public_url)
|
| 79 |
+
nest_asyncio.apply()
|
| 80 |
+
uvicorn.run(app, port=8000)
|