File size: 5,842 Bytes
9c8703c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# from fastapi import FastAPI
# from fastapi.middleware.cors import CORSMiddleware
# from pydantic import BaseModel
# from typing import List

# from run_pipeline import format_survey_context, parse_topics_to_preferences
# from retriever import DefaultRetriever
# from tour_generator import TourGuideGenerator
# from query_rewritter import generate_human_query

# #uvicorn tour_api:app --reload


# import sys
# import os
# sys.path.append(os.path.join(os.path.dirname(__file__), "src_ubc"))



# app = FastAPI()

# # Enable CORS
# app.add_middleware(
#     CORSMiddleware,
#     allow_origins=["*"],  # or ["http://localhost:3000"] for stricter config
#     allow_credentials=True,
#     allow_methods=["*"],
#     allow_headers=["*"],
# )

# class SurveyInput(BaseModel):
#     major: str
#     age_group: str
#     class_subject: str
#     topics_of_interest: List[str]
#     exhibit_name: str
#     tour_length_minutes: int
#     additional_notes: str = ""

# @app.post("/generate")
# def generate_outputs(survey: SurveyInput):
#     survey_dict = survey.dict()
#     print("πŸ§ͺ Survey Dict:", survey_dict)

#     context = format_survey_context(survey_dict)
#     print("πŸ“„ Context:", context)

#     preferences = parse_topics_to_preferences(survey_dict["topics_of_interest"])
#     print("🎯 Preferences:", preferences)

#     preferences.exhibits = [survey_dict["exhibit_name"]]
    
#     rewritten_query = generate_human_query(preferences)
#     print("✏️ Rewritten Query:", rewritten_query)

#     retriever = DefaultRetriever()
#     relevant_chunks = retriever._retrieve_with_text(rewritten_query, preferences, k=5)
#     print("πŸ“š Retrieved Chunks:", relevant_chunks)

#     generator = TourGuideGenerator()

#     return {
#         "itinerary": generator.generate(
#             "itinerary",
#             context,
#             relevant_chunks,
#             survey_id="api",
#             tour_length_minutes=survey_dict["tour_length_minutes"] 
#         ),
#         "talking_points": generator.generate(
#             prompt_type="talking_points",
#             context=context,
#             exhibit_chunks=relevant_chunks,
#             survey_id="api"
#         ),
#         "engagement_tips": generator.generate(
#             prompt_type="engagement_tips",
#             context=context,
#             exhibit_chunks=relevant_chunks,
#             survey_id="api"
#         )
#     }

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from typing import List

from backend.run_pipeline import format_survey_context, parse_topics_to_preferences
from backend.retriever import DefaultRetriever
from backend.tour_generator import TourGuideGenerator
from backend.query_rewritter import generate_human_query

import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), "src_ubc"))

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # or ["http://localhost:3000"] for stricter config
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class SurveyInput(BaseModel):
    major: str
    age_group: str
    class_subject: str
    topics_of_interest: List[str]
    exhibit_name: str
    tour_length_minutes: int
    additional_notes: str = ""

@app.post("/generate")
def generate_outputs(survey: SurveyInput):
    survey_dict = survey.dict()
    print("πŸ§ͺ Survey Dict:", survey_dict)

    context = format_survey_context(survey_dict)
    print("πŸ“„ Context:", context)

    preferences = parse_topics_to_preferences(survey_dict["topics_of_interest"])
    print("🎯 Preferences:", preferences)

    preferences.exhibits = [survey_dict["exhibit_name"]]
    
    rewritten_query = generate_human_query(preferences)
    print("✏️ Rewritten Query:", rewritten_query)

    retriever = DefaultRetriever()
    relevant_chunks = retriever._retrieve_with_text(rewritten_query, preferences, k=5)
    print("πŸ“š Retrieved Chunks:", relevant_chunks)

    generator = TourGuideGenerator()

    return {
        "itinerary": generator.generate(
            prompt_type="itinerary",
            context=context,
            exhibit_chunks=relevant_chunks,
            survey_id="api",
            major=survey_dict["major"],
            age_group=survey_dict["age_group"],
            class_subject=survey_dict["class_subject"],
            topics_of_interest=survey_dict["topics_of_interest"],
            exhibit_name=survey_dict["exhibit_name"],
            tour_length_minutes=survey_dict["tour_length_minutes"],
            additional_notes=survey_dict["additional_notes"]
        ),
        "talking_points": generator.generate(
            prompt_type="talking_points",
            context=context,
            exhibit_chunks=relevant_chunks,
            survey_id="api",
            major=survey_dict["major"],
            age_group=survey_dict["age_group"],
            class_subject=survey_dict["class_subject"],
            topics_of_interest=survey_dict["topics_of_interest"],
            exhibit_name=survey_dict["exhibit_name"],
            tour_length_minutes=survey_dict["tour_length_minutes"],
            additional_notes=survey_dict["additional_notes"]
        ),
        "engagement_tips": generator.generate(
            prompt_type="engagement_tips",
            context=context,
            exhibit_chunks=relevant_chunks,
            survey_id="api",
            major=survey_dict["major"],
            age_group=survey_dict["age_group"],
            class_subject=survey_dict["class_subject"],
            topics_of_interest=survey_dict["topics_of_interest"],
            exhibit_name=survey_dict["exhibit_name"],
            tour_length_minutes=survey_dict["tour_length_minutes"],
            additional_notes=survey_dict["additional_notes"]
        )
    }