quantumbit commited on
Commit
bfc1376
·
1 Parent(s): 5c6382d

"end" based trigger for ai implemented

Browse files
Files changed (3) hide show
  1. main.py +39 -1
  2. models.py +13 -1
  3. requirements.txt +2 -1
main.py CHANGED
@@ -1,7 +1,8 @@
1
  from fastapi import FastAPI, Depends, HTTPException
 
2
  from sqlalchemy.orm import Session
3
  from database import Base, engine
4
- from models import User, Log
5
  from schemas import SignupRequest, LoginRequest, LogRequest
6
  from auth import hash_password, verify_password, create_token
7
  from deps import get_db, get_current_user
@@ -57,4 +58,41 @@ def add_log(
57
  db.add(log)
58
  db.commit()
59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  return {"message": "Log stored"}
 
1
  from fastapi import FastAPI, Depends, HTTPException
2
+ import requests
3
  from sqlalchemy.orm import Session
4
  from database import Base, engine
5
+ from models import User, Log, CtrReport
6
  from schemas import SignupRequest, LoginRequest, LogRequest
7
  from auth import hash_password, verify_password, create_token
8
  from deps import get_db, get_current_user
 
58
  db.add(log)
59
  db.commit()
60
 
61
+ if "<END>" in data.log:
62
+ session_logs = (
63
+ db.query(Log)
64
+ .filter(Log.user_id == user_id, Log.session_id == data.session_id)
65
+ .order_by(Log.timestamp.asc())
66
+ .all()
67
+ )
68
+
69
+ combined_logs = "\n".join(
70
+ f"{entry.timestamp.isoformat()} : {entry.log}" for entry in session_logs
71
+ )
72
+
73
+ try:
74
+ response = requests.post(
75
+ "https://devbytes-og-ctrs.hf.space/processed-logs",
76
+ json={"logs": combined_logs},
77
+ timeout=30
78
+ )
79
+ except requests.RequestException as exc:
80
+ raise HTTPException(502, f"Log processing failed: {exc}")
81
+
82
+ if response.status_code != 200:
83
+ raise HTTPException(502, "Log processing service error")
84
+
85
+ payload = response.json()
86
+ report = CtrReport(
87
+ user_id=user_id,
88
+ session_id=data.session_id,
89
+ report=payload.get("report", ""),
90
+ insights=payload.get("insights", ""),
91
+ state_flow=payload.get("state_flow", ""),
92
+ suggestions=payload.get("suggestions", "")
93
+ )
94
+
95
+ db.add(report)
96
+ db.commit()
97
+
98
  return {"message": "Log stored"}
models.py CHANGED
@@ -18,4 +18,16 @@ class Log(Base):
18
  user_id = Column(Integer, nullable=False)
19
  session_id = Column(String, nullable=False)
20
  log = Column(Text, nullable=False)
21
- timestamp = Column(DateTime, default=datetime.utcnow)
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  user_id = Column(Integer, nullable=False)
19
  session_id = Column(String, nullable=False)
20
  log = Column(Text, nullable=False)
21
+ timestamp = Column(DateTime, default=datetime.utcnow)
22
+
23
+
24
+ class CtrReport(Base):
25
+ __tablename__ = "ctr_report"
26
+
27
+ id = Column(Integer, primary_key=True, index=True)
28
+ user_id = Column(Integer, nullable=False)
29
+ session_id = Column(String, nullable=False)
30
+ report = Column(Text, nullable=False)
31
+ insights = Column(Text, nullable=False)
32
+ state_flow = Column(Text, nullable=False)
33
+ suggestions = Column(Text, nullable=False)
requirements.txt CHANGED
@@ -4,4 +4,5 @@ sqlalchemy
4
  psycopg2-binary
5
  passlib[bcrypt]
6
  python-jose
7
- dotenv
 
 
4
  psycopg2-binary
5
  passlib[bcrypt]
6
  python-jose
7
+ dotenv
8
+ requests