Update main.py
Browse files
main.py
CHANGED
|
@@ -1,89 +1,89 @@
|
|
| 1 |
-
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
import google.generativeai as genai
|
| 4 |
-
import os
|
| 5 |
-
import requests
|
| 6 |
-
import pandas as pd
|
| 7 |
-
import validators
|
| 8 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 9 |
-
|
| 10 |
-
from dotenv import load_dotenv
|
| 11 |
-
load_dotenv()
|
| 12 |
-
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 13 |
-
df=pd.read_csv("salaries.csv")
|
| 14 |
-
|
| 15 |
-
app = FastAPI()
|
| 16 |
-
|
| 17 |
-
origins=[
|
| 18 |
-
"http://localhost:5173",
|
| 19 |
-
|
| 20 |
-
]
|
| 21 |
-
|
| 22 |
-
app.add_middleware(
|
| 23 |
-
CORSMiddleware,
|
| 24 |
-
allow_origins=
|
| 25 |
-
allow_credentials=True,
|
| 26 |
-
allow_methods=['*'],
|
| 27 |
-
allow_headers=['*']
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
model=genai.GenerativeModel("gemini-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
df_combined = df.astype(str).apply(lambda x: ' '.join(x), axis=1).tolist()
|
| 34 |
-
|
| 35 |
-
vectorizer = TfidfVectorizer()
|
| 36 |
-
X = vectorizer.fit_transform(df_combined)
|
| 37 |
-
|
| 38 |
-
def genetate_gemini_content(prompt,content):
|
| 39 |
-
response=model.generate_content([prompt,content])
|
| 40 |
-
return response.text
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
work_year = df['work_year']
|
| 44 |
-
job_titles = df['job_title']
|
| 45 |
-
salaries = df['salary']
|
| 46 |
-
experience_level = df['experience_level']
|
| 47 |
-
employment_type = df['employment_type']
|
| 48 |
-
salary_in_usd = df['salary_in_usd']
|
| 49 |
-
company_size = df['company_size']
|
| 50 |
-
|
| 51 |
-
# Combine all columns as strings for vectorization (if needed)
|
| 52 |
-
df_combined = df.astype(str).apply(lambda x: ' '.join(x), axis=1).tolist()
|
| 53 |
-
|
| 54 |
-
# Use TF-IDF Vectorizer for embeddings
|
| 55 |
-
vectorizer = TfidfVectorizer()
|
| 56 |
-
X = vectorizer.fit_transform(df_combined)
|
| 57 |
-
|
| 58 |
-
prompt = f"""
|
| 59 |
-
|
| 60 |
-
I have a dataset with the following salary information:
|
| 61 |
-
|
| 62 |
-
Work Year: {work_year.tolist()}
|
| 63 |
-
Job Titles: {job_titles.tolist()}
|
| 64 |
-
Salaries (in USD): {salary_in_usd.tolist()}
|
| 65 |
-
Experience Level: {experience_level.tolist()}
|
| 66 |
-
Employment Type: {employment_type.tolist()}
|
| 67 |
-
Company Size: {company_size.tolist()}
|
| 68 |
-
|
| 69 |
-
Based on this data, can you answer the following question:
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
Please provide a short and direct answer based on the data. No extra explanations are needed, just the answer less than 2 lines and dont use "\ n" or any special charcher other than related to the data .
|
| 73 |
-
"""
|
| 74 |
-
|
| 75 |
-
@app.get("/hi")
|
| 76 |
-
async def ee():
|
| 77 |
-
return {"de":"hehe"}
|
| 78 |
-
|
| 79 |
-
@app.post("/webqa")
|
| 80 |
-
async def webchat(question:str):
|
| 81 |
-
try:
|
| 82 |
-
response=model.generate_content([prompt,question])
|
| 83 |
-
print(response.text)
|
| 84 |
-
return response.text
|
| 85 |
-
|
| 86 |
-
except:
|
| 87 |
-
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
import os
|
| 5 |
+
import requests
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import validators
|
| 8 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 9 |
+
|
| 10 |
+
from dotenv import load_dotenv
|
| 11 |
+
load_dotenv()
|
| 12 |
+
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
|
| 13 |
+
df=pd.read_csv("salaries.csv")
|
| 14 |
+
|
| 15 |
+
app = FastAPI()
|
| 16 |
+
|
| 17 |
+
origins=[
|
| 18 |
+
"http://localhost:5173",
|
| 19 |
+
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
app.add_middleware(
|
| 23 |
+
CORSMiddleware,
|
| 24 |
+
allow_origins=['*'],
|
| 25 |
+
allow_credentials=True,
|
| 26 |
+
allow_methods=['*'],
|
| 27 |
+
allow_headers=['*']
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
model=genai.GenerativeModel("gemini-2.0-flash-lite")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
df_combined = df.astype(str).apply(lambda x: ' '.join(x), axis=1).tolist()
|
| 34 |
+
|
| 35 |
+
vectorizer = TfidfVectorizer()
|
| 36 |
+
X = vectorizer.fit_transform(df_combined)
|
| 37 |
+
|
| 38 |
+
def genetate_gemini_content(prompt,content):
|
| 39 |
+
response=model.generate_content([prompt,content])
|
| 40 |
+
return response.text
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
work_year = df['work_year']
|
| 44 |
+
job_titles = df['job_title']
|
| 45 |
+
salaries = df['salary']
|
| 46 |
+
experience_level = df['experience_level']
|
| 47 |
+
employment_type = df['employment_type']
|
| 48 |
+
salary_in_usd = df['salary_in_usd']
|
| 49 |
+
company_size = df['company_size']
|
| 50 |
+
|
| 51 |
+
# Combine all columns as strings for vectorization (if needed)
|
| 52 |
+
df_combined = df.astype(str).apply(lambda x: ' '.join(x), axis=1).tolist()
|
| 53 |
+
|
| 54 |
+
# Use TF-IDF Vectorizer for embeddings
|
| 55 |
+
vectorizer = TfidfVectorizer()
|
| 56 |
+
X = vectorizer.fit_transform(df_combined)
|
| 57 |
+
|
| 58 |
+
prompt = f"""
|
| 59 |
+
|
| 60 |
+
I have a dataset with the following salary information:
|
| 61 |
+
|
| 62 |
+
Work Year: {work_year.tolist()}
|
| 63 |
+
Job Titles: {job_titles.tolist()}
|
| 64 |
+
Salaries (in USD): {salary_in_usd.tolist()}
|
| 65 |
+
Experience Level: {experience_level.tolist()}
|
| 66 |
+
Employment Type: {employment_type.tolist()}
|
| 67 |
+
Company Size: {company_size.tolist()}
|
| 68 |
+
|
| 69 |
+
Based on this data, can you answer the following question:
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
Please provide a short and direct answer based on the data. No extra explanations are needed, just the answer less than 2 lines and dont use "\ n" or any special charcher other than related to the data .
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
@app.get("/hi")
|
| 76 |
+
async def ee():
|
| 77 |
+
return {"de":"hehe"}
|
| 78 |
+
|
| 79 |
+
@app.post("/webqa")
|
| 80 |
+
async def webchat(question:str):
|
| 81 |
+
try:
|
| 82 |
+
response=model.generate_content([prompt,question])
|
| 83 |
+
print(response.text)
|
| 84 |
+
return response.text
|
| 85 |
+
|
| 86 |
+
except:
|
| 87 |
+
raise HTTPException(status_code=500, detail="Internal Server Error")
|
| 88 |
+
|
| 89 |
+
|