File size: 2,604 Bytes
7f0653e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc93f64
 
 
 
 
 
 
 
 
 
 
 
7f0653e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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()
	# name : VonNeumann, hacker_laws,
	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])