|
|
from fastapi import FastAPI |
|
|
from gemiwrap import GeminiWrapper |
|
|
from google import genai |
|
|
from google.genai import types |
|
|
import json_repair |
|
|
from functools import partial |
|
|
import os |
|
|
if os.path.exists(".env"): |
|
|
from dotenv import load_dotenv |
|
|
load_dotenv() |
|
|
|
|
|
app = FastAPI() |
|
|
|
|
|
geminiWrapper = partial(GeminiWrapper, |
|
|
model_name="gemini-flash-lite-latest", |
|
|
schema=genai.types.Schema( |
|
|
type = genai.types.Type.OBJECT, |
|
|
required = ["title", "content"], |
|
|
properties = { |
|
|
"title": genai.types.Schema( |
|
|
type = genai.types.Type.STRING, |
|
|
), |
|
|
"content": genai.types.Schema( |
|
|
type = genai.types.Type.STRING, |
|
|
), |
|
|
}, |
|
|
), |
|
|
delete_files=True |
|
|
) |
|
|
|
|
|
@app.get("/") |
|
|
def greet_json(): |
|
|
return { |
|
|
"available_endpoints": [ |
|
|
"http://127.0.0.1:8000/", |
|
|
"http://127.0.0.1:8000/breaking_news", |
|
|
"http://127.0.0.1:8000/hacker_news", |
|
|
"http://127.0.0.1:8000/hackers_law", |
|
|
"http://127.0.0.1:8000/pdf_crux?name=assembly", |
|
|
"http://127.0.0.1:8000/pdf_crux?name=arm", |
|
|
"http://127.0.0.1:8000/pdf_crux?name=cpumemory", |
|
|
"http://127.0.0.1:8000/pdf_crux?name=c" |
|
|
] |
|
|
} |
|
|
|
|
|
@app.get("/breaking_news") |
|
|
def breaking_news(): |
|
|
user_prompt = None |
|
|
with open("breaking_news.md", 'r') as file: |
|
|
user_prompt = file.read() |
|
|
|
|
|
grounding_tool = types.Tool( |
|
|
google_search=types.GoogleSearch() |
|
|
) |
|
|
model_responses = geminiWrapper(tools=[grounding_tool], response_mime_type="text/plain").send_message(user_prompt=user_prompt) |
|
|
return json_repair.loads(model_responses[0]) |
|
|
|
|
|
@app.get("/hacker_news") |
|
|
def hacker_news(): |
|
|
with open("test.txt", 'w') as file: |
|
|
file.write("Hey, Hey") |
|
|
|
|
|
text = None |
|
|
with open("test.txt", 'r') as file: |
|
|
text = file.read() |
|
|
return {"Hello": text} |
|
|
|
|
|
@app.get("/hackers_law") |
|
|
def hackers_law(): |
|
|
user_prompt = None |
|
|
with open("hackers_law.md", 'r') as file: |
|
|
user_prompt = file.read() |
|
|
|
|
|
model_responses = geminiWrapper().send_message(user_prompt=user_prompt) |
|
|
return json_repair.loads(model_responses[0]) |
|
|
|
|
|
@app.get("/pdf_crux") |
|
|
def pdf_crux(name: str): |
|
|
system_prompt = None |
|
|
with open("LearningPointsExtractor.md", 'r') as file: |
|
|
system_prompt = file.read() |
|
|
|
|
|
file_path = "ProgrammingGroundUp-1-0-booksize.pdf" |
|
|
if name == "assembly": |
|
|
file_path = "ProgrammingGroundUp-1-0-booksize.pdf" |
|
|
elif name == "arm": |
|
|
file_path = "arm-baremetal-ebook.pdf" |
|
|
elif name == "cpumemory": |
|
|
file_path = "cpumemory.pdf" |
|
|
elif name == "c": |
|
|
file_path = "The C Programming Language (Kernighan Ritchie).pdf" |
|
|
|
|
|
model_responses = geminiWrapper().send_message(user_prompt="", system_instruction=system_prompt, file_path=file_path) |
|
|
return json_repair.loads(model_responses[0]) |