Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
39 changes: 38 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,39 @@
# Python cache and compiled files
__pycache__/
*.pyc
*.pyo

# Virtual environments
venv/
.venv/

# Environment variables and secrets
.env
local-readme
.env.*

# Local documentation or notes
local-readme

# macOS system files
.DS_Store

# VS Code settings
.vscode/

# Log files
*.log

# Python packaging
*.egg-info/
dist/
build/

# Jupyter Notebook checkpoints
.ipynb_checkpoints/

# PyCharm settings
.idea/

# Coverage reports
htmlcov/
.coverage
80 changes: 24 additions & 56 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,15 @@
import os
from pyexpat import model
import sys
from fastapi import FastAPI
from pydantic import BaseModel
import subprocess
import json
from pymongo import MongoClient
import uvicorn
from mongodb import users_collection

app = FastAPI()

class InputData(BaseModel):
user_id: str
query: str

# MONGO_URI = os.getenv("MONGO_URI", "mongodb://localhost:27017/")
client = MongoClient("mongodb+srv://trmnteam:tBp54siAioeGkVpb@cluster0.68spx5t.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0")
db = client["chat_db"]
history_collection = db["chat_history"]
user_ID: str

@app.get("/")
def read_root():
Expand All @@ -25,7 +18,6 @@ def read_root():
@app.get("/run-script")
def run_script():
try:
# Run the chat.py script
result = subprocess.run(
['python', 'chat.py'],
capture_output=True,
Expand All @@ -41,56 +33,32 @@ def run_script():
@app.post("/ask-ai")
def ask_ai(data: InputData):
try:
docs = list(users_collection.find({"user_id": data.user_ID}))
if docs:
history = []
for doc in docs:
history.append({
"user_prompt": doc.get("user_promt", ""),
"AI": doc.get("AI", "")
})
print("History loaded from MongoDB:", history)
else:
history = []
payload = json.dumps({"query": data.query, "history": history})
result = subprocess.run(
['python', 'chat.py', data.user_id, data.query],
['python', 'chat.py', payload],
capture_output=True,
text=True
)
response_text = result.stdout.strip()
error_text = result.stderr.strip()

# Load existing history
history = load_history(data.user_id)

# Append the user's input and the AI's response to the history
history.append({"role": "user", "parts": [data.query]})
history.append({"role": "model", "parts": [response_text]})

# Save the updated history
save_history(data.user_id, history)

return {"response": response_text, "error": error_text}
if result.stdout.strip():
users_collection.insert_one({
"user_id": data.user_ID,
"user_promt": data.query,
"AI": result.stdout.strip()
})
return {"response": result.stdout.strip(), "error": result.stderr.strip()}
except Exception as e:
return {"error": str(e)}

if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)

# db connection and other configurations

def load_history(user_id):
doc = history_collection.find_one({"user_id": user_id})
if doc and "history" in doc:
return doc["history"]
return []

def save_history(user_id, history):
history_collection.update_one(
{"user_id": user_id},
{"$set": {"history": history}},
upsert=True
)

if len(sys.argv) > 2:
user_id = sys.argv[1]
user_input = sys.argv[2]
history = load_history(user_id)
chat_session = model.start_chat(history=history)
response = chat_session.send_message(user_input)
history.append({"role": "user", "parts": [user_input]})
history.append({"role": "model", "parts": [response.text]})
save_history(user_id, history)
print(response.text)
else:
print("Error: No user_id or input provided.")
uvicorn.run(app, host="0.0.0.0", port=8000)
17 changes: 17 additions & 0 deletions mongodb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pymongo
from dotenv import load_dotenv
import os

load_dotenv()
MONGO_URI = os.getenv("MONGO_URI")
DB_NAME = "CODE_GENERATOR"

try:
client = pymongo.MongoClient(MONGO_URI, serverSelectionTimeoutMS=3000)
client.admin.command('ping')
print("MongoDB connection: SUCCESS")
except Exception as e:
print(f"MongoDB connection: FAILED ({e})")

db = client[DB_NAME]
users_collection = db["users"]
Binary file not shown.
Binary file not shown.
222 changes: 0 additions & 222 deletions venv/Lib/site-packages/_distutils_hack/__init__.py

This file was deleted.

Binary file not shown.
Binary file not shown.
Loading