removed track
Browse files
app.py
CHANGED
|
@@ -96,7 +96,7 @@ Answer:
|
|
| 96 |
def generate_agent_answer(context: str, question: str) -> str:
|
| 97 |
prompt = get_prompt_plain(context, question)
|
| 98 |
response = genai_client.models.generate_content(
|
| 99 |
-
model="gemini-2.0-flash",
|
| 100 |
contents=prompt,
|
| 101 |
config=types.GenerateContentConfig(
|
| 102 |
temperature=0.01,
|
|
@@ -119,92 +119,27 @@ import uuid
|
|
| 119 |
import os
|
| 120 |
|
| 121 |
|
| 122 |
-
|
| 123 |
-
DATASET_NAME = "Jasur05/inha-chat-logs"
|
| 124 |
-
|
| 125 |
-
def log_to_dataset(question, answer, response_time_ms=None):
|
| 126 |
-
"""Log interaction to HuggingFace Dataset (permanent storage)"""
|
| 127 |
-
try:
|
| 128 |
-
|
| 129 |
-
new_data = {
|
| 130 |
-
"timestamp": [datetime.now().isoformat()],
|
| 131 |
-
"session_id": [str(uuid.uuid4())[:8]],
|
| 132 |
-
"question": [question],
|
| 133 |
-
"answer": [answer],
|
| 134 |
-
"response_time_ms": [response_time_ms or 0]
|
| 135 |
-
}
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
try:
|
| 139 |
-
existing_dataset = load_dataset(DATASET_NAME, split="train", token=HF_TOKEN)
|
| 140 |
-
existing_df = existing_dataset.to_pandas()
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
new_df = pd.DataFrame(new_data)
|
| 144 |
-
combined_df = pd.concat([existing_df, new_df], ignore_index=True)
|
| 145 |
-
|
| 146 |
-
except Exception:
|
| 147 |
-
|
| 148 |
-
combined_df = pd.DataFrame(new_data)
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
dataset = Dataset.from_pandas(combined_df)
|
| 152 |
-
dataset.push_to_hub(
|
| 153 |
-
DATASET_NAME,
|
| 154 |
-
token=HF_TOKEN,
|
| 155 |
-
private=True # Keep your logs private
|
| 156 |
-
)
|
| 157 |
-
|
| 158 |
-
print(f"β
Logged to dataset: {question[:50]}...")
|
| 159 |
-
|
| 160 |
-
except Exception as e:
|
| 161 |
-
print(f"β Dataset logging failed: {e}")
|
| 162 |
-
# Fallback to local file
|
| 163 |
-
log_to_local_file(question, answer, response_time_ms)
|
| 164 |
-
|
| 165 |
-
def log_to_local_file(question, answer, response_time_ms):
|
| 166 |
-
|
| 167 |
-
try:
|
| 168 |
-
data = {
|
| 169 |
-
"timestamp": datetime.now().isoformat(),
|
| 170 |
-
"question": question,
|
| 171 |
-
"answer": answer,
|
| 172 |
-
"response_time_ms": response_time_ms
|
| 173 |
-
}
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
df = pd.DataFrame([data])
|
| 177 |
-
file_exists = os.path.exists("backup_logs.csv")
|
| 178 |
-
df.to_csv("backup_logs.csv", mode='a', header=not file_exists, index=False)
|
| 179 |
-
|
| 180 |
-
except Exception as e:
|
| 181 |
-
print(f"β Backup logging failed: {e}")
|
| 182 |
-
|
| 183 |
def answer_question(question):
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
if not question.strip():
|
| 188 |
-
|
| 189 |
-
response_time_ms = 0
|
| 190 |
-
else:
|
| 191 |
-
try:
|
| 192 |
-
answer = rag_answer(question, collection)
|
| 193 |
-
response_time_ms = (datetime.now() - start_time).total_seconds() * 1000
|
| 194 |
-
except Exception as e:
|
| 195 |
-
answer = f"Sorry, I encountered an error: {str(e)}"
|
| 196 |
-
response_time_ms = (datetime.now() - start_time).total_seconds() * 1000
|
| 197 |
-
|
| 198 |
-
log_to_dataset(question, answer, response_time_ms)
|
| 199 |
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
|
|
|
| 202 |
|
| 203 |
demo = gr.Interface(
|
| 204 |
fn=answer_question,
|
| 205 |
inputs=gr.Textbox(
|
| 206 |
label="Ask me anything about Inha University SGCSβ¦",
|
| 207 |
-
placeholder="e.g. How many Major Required credits should I take for graduation?",
|
| 208 |
lines=2
|
| 209 |
),
|
| 210 |
outputs=gr.Markdown(
|
|
@@ -212,7 +147,7 @@ demo = gr.Interface(
|
|
| 212 |
show_copy_button=True
|
| 213 |
),
|
| 214 |
title="π Inha University SGCS Info Assistant",
|
| 215 |
-
description="Get answers to your questions about Inha University SGCS.",
|
| 216 |
theme=gr.themes.Soft(),
|
| 217 |
examples=[
|
| 218 |
["What classes should I normally take as 3nd semester ISE student?"],
|
|
@@ -221,8 +156,7 @@ demo = gr.Interface(
|
|
| 221 |
]
|
| 222 |
)
|
| 223 |
|
| 224 |
-
|
| 225 |
-
demo.launch()
|
| 226 |
|
| 227 |
if __name__ == "__main__":
|
| 228 |
demo.launch(
|
|
|
|
| 96 |
def generate_agent_answer(context: str, question: str) -> str:
|
| 97 |
prompt = get_prompt_plain(context, question)
|
| 98 |
response = genai_client.models.generate_content(
|
| 99 |
+
model="gemini-2.0-flash-lite",
|
| 100 |
contents=prompt,
|
| 101 |
config=types.GenerateContentConfig(
|
| 102 |
temperature=0.01,
|
|
|
|
| 119 |
import os
|
| 120 |
|
| 121 |
|
| 122 |
+
# gradio interface code below
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
def answer_question(question):
|
| 124 |
+
"""
|
| 125 |
+
Main function that processes the question and returns the answer
|
| 126 |
+
"""
|
| 127 |
if not question.strip():
|
| 128 |
+
return "Please enter a question about Inha University."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
try:
|
| 131 |
+
answer = rag_answer(question, collection)
|
| 132 |
+
return answer
|
| 133 |
+
except Exception as e:
|
| 134 |
+
return f"Sorry, I encountered an error: {str(e)}"
|
| 135 |
|
| 136 |
+
# βββ 6. Gradio Frontend βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 137 |
|
| 138 |
demo = gr.Interface(
|
| 139 |
fn=answer_question,
|
| 140 |
inputs=gr.Textbox(
|
| 141 |
label="Ask me anything about Inha University SGCSβ¦",
|
| 142 |
+
placeholder="e.g. How many Major Required credits should I take for graduation? ",
|
| 143 |
lines=2
|
| 144 |
),
|
| 145 |
outputs=gr.Markdown(
|
|
|
|
| 147 |
show_copy_button=True
|
| 148 |
),
|
| 149 |
title="π Inha University SGCS Info Assistant",
|
| 150 |
+
description="Get answers to your questions about Inha University SGCS .",
|
| 151 |
theme=gr.themes.Soft(),
|
| 152 |
examples=[
|
| 153 |
["What classes should I normally take as 3nd semester ISE student?"],
|
|
|
|
| 156 |
]
|
| 157 |
)
|
| 158 |
|
| 159 |
+
|
|
|
|
| 160 |
|
| 161 |
if __name__ == "__main__":
|
| 162 |
demo.launch(
|