rohangbs commited on
Commit
9a1ff0e
·
1 Parent(s): 69d1d3d
Files changed (1) hide show
  1. app.py +8 -7
app.py CHANGED
@@ -1,15 +1,12 @@
1
  import os
2
  import sqlite3
3
  from fastapi import FastAPI, Request, Depends, HTTPException
4
- from fastapi.responses import JSONResponse, RedirectResponse
5
  from pydantic import BaseModel
6
- from functools import wraps
7
  import hashlib
8
  from textblob import TextBlob
9
  from groq import Groq
10
  from starlette.middleware.sessions import SessionMiddleware
11
- from starlette.responses import RedirectResponse
12
- from starlette.staticfiles import StaticFiles
13
 
14
  app = FastAPI()
15
 
@@ -47,6 +44,7 @@ def init_db():
47
  conn.close()
48
 
49
  init_db()
 
50
  # Helper functions
51
  def hash_password(password):
52
  return hashlib.sha256(password.encode()).hexdigest()
@@ -56,7 +54,7 @@ def get_sentiment(text):
56
  return analysis.sentiment.polarity
57
 
58
  def store_chat(user_id, user_message, bot_response, sentiment_score):
59
- conn = sqlite3.connect('chat_history.db')
60
  c = conn.cursor()
61
  c.execute('''INSERT INTO chats (user_id, user_message, bot_response, sentiment_score)
62
  VALUES (?, ?, ?, ?)''', (user_id, user_message, bot_response, sentiment_score))
@@ -92,6 +90,10 @@ def generate_response(prompt):
92
  class UserMessage(BaseModel):
93
  message: str
94
 
 
 
 
 
95
  @app.post("/chat")
96
  async def chat_endpoint(request: Request, message: UserMessage):
97
  user_message = message.message
@@ -101,7 +103,7 @@ async def chat_endpoint(request: Request, message: UserMessage):
101
  raise HTTPException(status_code=401, detail="User not logged in")
102
 
103
  # Get user's recent chat history for context
104
- conn = sqlite3.connect('chat_history.db')
105
  c = conn.cursor()
106
  c.execute('''SELECT user_message, bot_response
107
  FROM chats
@@ -127,4 +129,3 @@ async def chat_endpoint(request: Request, message: UserMessage):
127
  })
128
  except Exception as e:
129
  raise HTTPException(status_code=500, detail=f"I apologize, but I encountered an error: {str(e)}")
130
-
 
1
  import os
2
  import sqlite3
3
  from fastapi import FastAPI, Request, Depends, HTTPException
4
+ from fastapi.responses import JSONResponse
5
  from pydantic import BaseModel
 
6
  import hashlib
7
  from textblob import TextBlob
8
  from groq import Groq
9
  from starlette.middleware.sessions import SessionMiddleware
 
 
10
 
11
  app = FastAPI()
12
 
 
44
  conn.close()
45
 
46
  init_db()
47
+
48
  # Helper functions
49
  def hash_password(password):
50
  return hashlib.sha256(password.encode()).hexdigest()
 
54
  return analysis.sentiment.polarity
55
 
56
  def store_chat(user_id, user_message, bot_response, sentiment_score):
57
+ conn = sqlite3.connect(DATABASE_PATH) # Use the DATABASE_PATH defined above
58
  c = conn.cursor()
59
  c.execute('''INSERT INTO chats (user_id, user_message, bot_response, sentiment_score)
60
  VALUES (?, ?, ?, ?)''', (user_id, user_message, bot_response, sentiment_score))
 
90
  class UserMessage(BaseModel):
91
  message: str
92
 
93
+ @app.get("/")
94
+ async def root():
95
+ return {"message": "Welcome to Amara, your chatbot!"}
96
+
97
  @app.post("/chat")
98
  async def chat_endpoint(request: Request, message: UserMessage):
99
  user_message = message.message
 
103
  raise HTTPException(status_code=401, detail="User not logged in")
104
 
105
  # Get user's recent chat history for context
106
+ conn = sqlite3.connect(DATABASE_PATH) # Use the DATABASE_PATH defined above
107
  c = conn.cursor()
108
  c.execute('''SELECT user_message, bot_response
109
  FROM chats
 
129
  })
130
  except Exception as e:
131
  raise HTTPException(status_code=500, detail=f"I apologize, but I encountered an error: {str(e)}")