nicolaydef commited on
Commit
e38c68a
·
verified ·
1 Parent(s): d96864c

Delete main.py

Browse files
Files changed (1) hide show
  1. main.py +0 -92
main.py DELETED
@@ -1,92 +0,0 @@
1
- from fastapi import FastAPI, HTTPException
2
- from fastapi.staticfiles import StaticFiles
3
- from fastapi.responses import FileResponse
4
- from pydantic import BaseModel
5
- from supabase import create_client, Client
6
- import os
7
- import uvicorn
8
-
9
- # --- КОНФИГУРАЦИЯ ---
10
- app = FastAPI()
11
-
12
- # Получаем ключи (Настройте их в Settings -> Repository Secrets в HF)
13
- SUPABASE_URL = os.environ.get("SUPABASE_URL")
14
- SUPABASE_KEY = os.environ.get("SUPABASE_KEY")
15
-
16
- # Инициализация Supabase
17
- if SUPABASE_URL and SUPABASE_KEY:
18
- supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
19
- else:
20
- print("WARNING: Supabase keys not found!")
21
- supabase = None
22
-
23
- # Модели данных
24
- class Message(BaseModel):
25
- user_email: str
26
- role: str
27
- content: str
28
-
29
- # --- API ENDPOINTS ---
30
-
31
- @app.post("/api/chat")
32
- async def chat_endpoint(msg: Message):
33
- """
34
- 1. Сохраняет сообщение пользователя
35
- 2. Генерирует ответ (тут пока эхо)
36
- 3. Сохраняет ответ бота
37
- """
38
- if not supabase:
39
- raise HTTPException(status_code=500, detail="Database not connected")
40
-
41
- # 1. Сохраняем сообщение юзера
42
- try:
43
- supabase.table("chat_history").insert({
44
- "user_email": msg.user_email,
45
- "role": "user",
46
- "content": msg.content
47
- }).execute()
48
- except Exception as e:
49
- print(f"Error saving user msg: {e}")
50
-
51
- # 2. Логика ИИ (Здесь можно подключить HuggingFace Inference API или OpenAI)
52
- bot_response = f"Я получил ваше сообщение: '{msg.content}'. (Ответ от FastAPI сервера)"
53
-
54
- # 3. Сохраняем ответ бота
55
- try:
56
- supabase.table("chat_history").insert({
57
- "user_email": msg.user_email,
58
- "role": "assistant",
59
- "content": bot_response
60
- }).execute()
61
- except Exception as e:
62
- print(f"Error saving bot msg: {e}")
63
-
64
- return {"response": bot_response}
65
-
66
- @app.get("/api/history")
67
- async def get_history(email: str):
68
- """Получает историю переписки"""
69
- if not supabase:
70
- return []
71
-
72
- try:
73
- response = supabase.table("chat_history") \
74
- .select("*") \
75
- .eq("user_email", email) \
76
- .order("created_at", desc=False) \
77
- .execute()
78
- return response.data
79
- except Exception as e:
80
- print(f"Error fetching history: {e}")
81
- return []
82
-
83
- # --- РАЗДАЧА ФРОНТЕНДА ---
84
-
85
- # Раздаем главную страницу
86
- @app.get("/")
87
- async def read_index():
88
- return FileResponse('index.html')
89
-
90
- # Запуск (для локальной отладки, в Docker запускается через CMD)
91
- if __name__ == "__main__":
92
- uvicorn.run(app, host="0.0.0.0", port=7860)