pracheeeeez commited on
Commit
540c34c
Β·
verified Β·
1 Parent(s): d319d4a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -22
app.py CHANGED
@@ -5,6 +5,8 @@ from langchain.schema import Document
5
  from transformers import pipeline
6
  import pickle
7
  import time
 
 
8
 
9
  def load_faiss_index(index_file="faiss_course_index.pkl"):
10
  with open(index_file, "rb") as f:
@@ -19,7 +21,7 @@ def get_faiss_retriever(index_file="faiss_course_index.pkl"):
19
  def get_answer_from_t5(query, context):
20
  t5_pipeline = pipeline("text2text-generation", model="t5-small", device=0)
21
  input_text = f"Question: {query} Context: {context}"
22
- answer = t5_pipeline(input_text, max_length = 300,truncation = True)
23
  return answer[0]['generated_text']
24
 
25
 
@@ -46,19 +48,9 @@ def search_and_answer(query, index_file="faiss_course_index.pkl"):
46
 
47
  return response, course_info, summary
48
 
49
- import streamlit as st
50
- import time
51
-
52
- def create_card(title, lessons, image_url="https://via.placeholder.com/300x150", rating="4.0"):
53
- card_template = f"""
54
- <div style="width: 300px; margin: 10px; padding: 15px; background-color: #f9f9f9;
55
- border-radius: 8px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); text-align: center;">
56
- <img src="{image_url}" alt="Course Image" style="width: 100%; border-radius: 8px; margin-bottom: 10px;">
57
- <h4 style="margin: 10px 0;">{title}</h4>
58
- <p style="margin: 5px 0; font-size: 14px;">⭐ {rating} | {lessons} Lessons</p>
59
- </div>
60
- """
61
- return card_template
62
 
63
  def main():
64
  st.title("πŸ“š Course Search WebApp")
@@ -75,22 +67,17 @@ def main():
75
  st.success("βœ… Results fetched successfully!")
76
  st.write(f"⏱ **Time Taken:** {time.time() - start_time:.2f} seconds")
77
 
78
- # Display related courses as cards
79
  st.subheader("πŸŽ“ Related Courses:")
80
  st.write("<div style='display: flex; flex-wrap: wrap; justify-content: space-between;'>", unsafe_allow_html=True)
81
  for course_title, course_lessons in course_info:
82
- card_html = f"""
83
- <div style='width: 48%; padding: 10px; margin: 10px; border: 1px solid #f7f7f7; border-radius: 8px; box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);'>
84
- <h3 style='font-size: 16px; color: #edebeb;'>{course_title}</h3>
85
- <p style='font-size: 14px; color: #c9c9c9;'>Lessons: {course_lessons}</p>
86
- </div>
87
- """
88
  st.write(card_html, unsafe_allow_html=True)
89
  st.write("</div>", unsafe_allow_html=True)
90
 
91
  # Display summary with better layout
92
  st.subheader("πŸ“ Summary:")
93
- st.info(summary[:300])
94
 
95
  st.markdown("<p style='text-align: center;'>Developed with ❀️ by Prachi</p>", unsafe_allow_html=True)
96
 
 
5
  from transformers import pipeline
6
  import pickle
7
  import time
8
+ from string import Template
9
+
10
 
11
  def load_faiss_index(index_file="faiss_course_index.pkl"):
12
  with open(index_file, "rb") as f:
 
21
  def get_answer_from_t5(query, context):
22
  t5_pipeline = pipeline("text2text-generation", model="t5-small", device=0)
23
  input_text = f"Question: {query} Context: {context}"
24
+ answer = t5_pipeline(input_text, max_length = 500,truncation = True)
25
  return answer[0]['generated_text']
26
 
27
 
 
48
 
49
  return response, course_info, summary
50
 
51
+ def load_card_template():
52
+ with open("card_template.html",'r') as file:
53
+ return file.read()
 
 
 
 
 
 
 
 
 
 
54
 
55
  def main():
56
  st.title("πŸ“š Course Search WebApp")
 
67
  st.success("βœ… Results fetched successfully!")
68
  st.write(f"⏱ **Time Taken:** {time.time() - start_time:.2f} seconds")
69
 
70
+ card_template = load_card_template()
71
  st.subheader("πŸŽ“ Related Courses:")
72
  st.write("<div style='display: flex; flex-wrap: wrap; justify-content: space-between;'>", unsafe_allow_html=True)
73
  for course_title, course_lessons in course_info:
74
+ card_html = Template(card_template).substitute(course_title = course_title, course_lessons = course_lessons)
 
 
 
 
 
75
  st.write(card_html, unsafe_allow_html=True)
76
  st.write("</div>", unsafe_allow_html=True)
77
 
78
  # Display summary with better layout
79
  st.subheader("πŸ“ Summary:")
80
+ st.info(summary[:500])
81
 
82
  st.markdown("<p style='text-align: center;'>Developed with ❀️ by Prachi</p>", unsafe_allow_html=True)
83