forked from alvinbengeorge/3DUCAT0R5
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
81 lines (66 loc) · 2.16 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import google.generativeai as genai
from dotenv import load_dotenv
from pydantic import BaseModel
import os
import requests
load_dotenv()
app = FastAPI()
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
model=genai.GenerativeModel("gemini-pro")
chat = model.start_chat(history=[])
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
students = [{
"username": "alvin",
"password": "alvin",
"recently_viewed": []
}]
class Item(BaseModel):
question: str
class Student(BaseModel):
username: str
password: str
class Location(BaseModel):
screen: str
username: str
def get_gemini_response(question: str):
response=chat.send_message(question,stream=True)
return response
@app.post("/gemini")
async def gemini(question: Item):
response = get_gemini_response(question.question)
response.resolve()
return { "response": "\n".join([chunk.text for chunk in response])}
@app.post("/wolfram")
async def wolfram(question: Item):
response = requests.get(f"https://api.wolframalpha.com/v1/spoken?i={question.question}&appid={os.getenv('WOLFRAM')}")
return { "response": response.content.decode() }
@app.post("/create-student")
async def create_student(student: Student):
student = dict(student)
student.update({ "recently_viewed": []})
students.append(student)
return { "response": "Student created", "student": student }
@app.post("/visited")
async def visited(location: Location):
for student in students:
if student["username"] == location.username:
student["recently_viewed"].append(location.screen)
return { "response": "Visited updated", "student": student }
return { "response": "Student not found" }
@app.get("/students")
async def get_students():
return { "students": students }
@app.get("/student/{username}")
async def get_student(username: str):
for student in students:
if student["username"] == username:
return { "student": student }
return { "response": "Student not found" }