Spaces:
Running
Running
Commit
·
f1fb734
1
Parent(s):
35a826f
app as fast apis
Browse files- .gitignore +7 -3
- app.py +70 -59
- requirements.txt +2 -2
.gitignore
CHANGED
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
# Byte-compiled / optimized / DLL files
|
2 |
__pycache__/
|
3 |
*.py[cod]
|
@@ -73,6 +79,4 @@ htmlcov/
|
|
73 |
*.sock
|
74 |
|
75 |
# Ignore any large files not tracked by Git LFS
|
76 |
-
!*.lfs
|
77 |
-
|
78 |
-
.env
|
|
|
1 |
+
#custom
|
2 |
+
.env
|
3 |
+
*.pdf
|
4 |
+
app copy.py
|
5 |
+
myenv/
|
6 |
+
|
7 |
# Byte-compiled / optimized / DLL files
|
8 |
__pycache__/
|
9 |
*.py[cod]
|
|
|
79 |
*.sock
|
80 |
|
81 |
# Ignore any large files not tracked by Git LFS
|
82 |
+
!*.lfs
|
|
|
|
app.py
CHANGED
@@ -1,40 +1,48 @@
|
|
1 |
-
from fastapi import FastAPI, Request
|
2 |
-
from fastapi.responses import RedirectResponse
|
3 |
-
from fastapi.templating import Jinja2Templates
|
4 |
-
from fastapi.staticfiles import StaticFiles
|
5 |
# from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
|
6 |
from helpmate_ai import initialize_conversation, retreive_results, rerank_with_cross_encoder, generate_response
|
7 |
-
import re
|
8 |
import google.generativeai as genai
|
9 |
import os
|
10 |
from dotenv import load_dotenv
|
|
|
11 |
|
12 |
-
#
|
13 |
-
# gemini_api_key = open("gemini_api_key.txt", "r").read().strip()
|
14 |
load_dotenv()
|
15 |
-
|
16 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
17 |
genai.configure(api_key=gemini_api_key)
|
18 |
|
19 |
# Initialize FastAPI app
|
20 |
app = FastAPI()
|
21 |
|
22 |
-
#
|
23 |
-
templates = Jinja2Templates(directory="templates")
|
24 |
-
|
25 |
-
# Serve static files (if needed)
|
26 |
-
app.mount("/static", StaticFiles(directory="static"), name="static")
|
27 |
-
|
28 |
-
# Enable CORS middleware if needed
|
29 |
# app.add_middleware(
|
30 |
# CORSMiddleware,
|
31 |
-
# allow_origins=["*"],
|
32 |
# allow_credentials=True,
|
33 |
# allow_methods=["*"],
|
34 |
# allow_headers=["*"],
|
35 |
# )
|
36 |
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
formatted_text = response_text.replace("\n", "<br>")
|
39 |
formatted_text = re.sub(r'(\*\*.*?\*\*)', r'<strong>\1</strong>', formatted_text).replace("**", "")
|
40 |
formatted_text = re.sub(r'(\d+\.\s)', r'<br><strong>\1</strong>', formatted_text)
|
@@ -44,53 +52,56 @@ def format_rag_response(response_text):
|
|
44 |
formatted_text = re.sub(r'\n\|\s*', r'<tr><td>', formatted_text)
|
45 |
return formatted_text
|
46 |
|
47 |
-
|
48 |
-
conversation = initialize_conversation()
|
49 |
-
|
50 |
-
# Initialize Gemini model
|
51 |
-
model = genai.GenerativeModel("gemini-1.5-flash", system_instruction=conversation)
|
52 |
-
|
53 |
-
def get_gemini_completions(conversation):
|
54 |
response = model.generate_content(conversation)
|
55 |
return response.text
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
@app.get("/")
|
62 |
-
async def default_func(request: Request):
|
63 |
global conversation_bot
|
64 |
-
return templates.TemplateResponse("index_invite.html", {"request": request, "name_xyz": conversation_bot})
|
65 |
-
|
66 |
-
@app.post("/end_conv")
|
67 |
-
async def end_conv():
|
68 |
-
global conversation_bot, conversation, top_3_laptops
|
69 |
-
conversation_bot = []
|
70 |
-
conversation = initialize_conversation()
|
71 |
introduction = get_gemini_completions(conversation)
|
72 |
-
conversation_bot
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
85 |
# Generate response
|
86 |
-
|
|
|
|
|
87 |
response_assistant = get_gemini_completions(messages)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
# Run the application
|
93 |
-
# if __name__ == '__main__':
|
94 |
-
# main()
|
95 |
-
# import uvicorn
|
96 |
-
# uvicorn.run(app, host="0.0.0.0", port=8000, debug=True)
|
|
|
1 |
+
from fastapi import FastAPI, Request
|
|
|
|
|
|
|
2 |
# from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
from pydantic import BaseModel
|
4 |
+
from typing import List, Optional
|
5 |
from helpmate_ai import initialize_conversation, retreive_results, rerank_with_cross_encoder, generate_response
|
|
|
6 |
import google.generativeai as genai
|
7 |
import os
|
8 |
from dotenv import load_dotenv
|
9 |
+
import re
|
10 |
|
11 |
+
# Load environment variables
|
|
|
12 |
load_dotenv()
|
|
|
13 |
gemini_api_key = os.getenv("GEMINI_API_KEY")
|
14 |
genai.configure(api_key=gemini_api_key)
|
15 |
|
16 |
# Initialize FastAPI app
|
17 |
app = FastAPI()
|
18 |
|
19 |
+
# # Enable CORS
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
# app.add_middleware(
|
21 |
# CORSMiddleware,
|
22 |
+
# allow_origins=["*"],
|
23 |
# allow_credentials=True,
|
24 |
# allow_methods=["*"],
|
25 |
# allow_headers=["*"],
|
26 |
# )
|
27 |
|
28 |
+
# Pydantic models for request/response validation
|
29 |
+
class Message(BaseModel):
|
30 |
+
role: str
|
31 |
+
content: str
|
32 |
+
|
33 |
+
class ChatRequest(BaseModel):
|
34 |
+
message: str
|
35 |
+
|
36 |
+
class ChatResponse(BaseModel):
|
37 |
+
response: str
|
38 |
+
conversation: List[Message]
|
39 |
+
|
40 |
+
# Initialize conversation and model
|
41 |
+
conversation_bot = []
|
42 |
+
conversation = initialize_conversation()
|
43 |
+
model = genai.GenerativeModel("gemini-1.5-flash", system_instruction=conversation)
|
44 |
+
|
45 |
+
def format_rag_response(response_text: str) -> str:
|
46 |
formatted_text = response_text.replace("\n", "<br>")
|
47 |
formatted_text = re.sub(r'(\*\*.*?\*\*)', r'<strong>\1</strong>', formatted_text).replace("**", "")
|
48 |
formatted_text = re.sub(r'(\d+\.\s)', r'<br><strong>\1</strong>', formatted_text)
|
|
|
52 |
formatted_text = re.sub(r'\n\|\s*', r'<tr><td>', formatted_text)
|
53 |
return formatted_text
|
54 |
|
55 |
+
def get_gemini_completions(conversation: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
response = model.generate_content(conversation)
|
57 |
return response.text
|
58 |
|
59 |
+
# Initialize conversation endpoint
|
60 |
+
@app.get("/init", response_model=ChatResponse)
|
61 |
+
async def initialize_chat():
|
|
|
|
|
|
|
62 |
global conversation_bot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
introduction = get_gemini_completions(conversation)
|
64 |
+
conversation_bot = [Message(role="bot", content=introduction)]
|
65 |
+
return ChatResponse(
|
66 |
+
response=introduction,
|
67 |
+
conversation=conversation_bot
|
68 |
+
)
|
69 |
+
|
70 |
+
# Chat endpoint
|
71 |
+
@app.post("/chat", response_model=ChatResponse)
|
72 |
+
async def chat(request: ChatRequest):
|
73 |
+
global conversation_bot
|
74 |
+
|
75 |
+
# Add user message to conversation
|
76 |
+
user_message = Message(role="user", content=request.message)
|
77 |
+
conversation_bot.append(user_message)
|
78 |
+
|
79 |
# Generate response
|
80 |
+
results_df = retreive_results(request.message)
|
81 |
+
top_docs = rerank_with_cross_encoder(request.message, results_df)
|
82 |
+
messages = generate_response(request.message, top_docs)
|
83 |
response_assistant = get_gemini_completions(messages)
|
84 |
+
formatted_response = format_rag_response(response_assistant)
|
85 |
+
|
86 |
+
# Add bot response to conversation
|
87 |
+
bot_message = Message(role="bot", content=formatted_response)
|
88 |
+
conversation_bot.append(bot_message)
|
89 |
+
|
90 |
+
return ChatResponse(
|
91 |
+
response=formatted_response,
|
92 |
+
conversation=conversation_bot
|
93 |
+
)
|
94 |
+
|
95 |
+
# Reset conversation endpoint
|
96 |
+
@app.post("/reset")
|
97 |
+
async def reset_conversation():
|
98 |
+
global conversation_bot, conversation
|
99 |
+
conversation_bot = []
|
100 |
+
conversation = initialize_conversation()
|
101 |
+
introduction = get_gemini_completions(conversation)
|
102 |
+
conversation_bot.append(Message(role="bot", content=introduction))
|
103 |
+
return {"status": "success", "message": "Conversation reset"}
|
104 |
|
105 |
+
# if __name__ == "__main__":
|
106 |
+
# import uvicorn
|
107 |
+
# uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -6,5 +6,5 @@ fastapi
|
|
6 |
uvicorn[standard]
|
7 |
jinja2
|
8 |
python-multipart
|
9 |
-
|
10 |
-
python-dotenv
|
|
|
6 |
uvicorn[standard]
|
7 |
jinja2
|
8 |
python-multipart
|
9 |
+
sentence-transformers
|
10 |
+
python-dotenv
|