Jasur05 commited on
Commit
4d9fdc7
Β·
verified Β·
1 Parent(s): 6d66025

make chatbot

Browse files
Files changed (1) hide show
  1. app.py +39 -32
app.py CHANGED
@@ -1,44 +1,43 @@
1
  import os
2
  from dotenv import load_dotenv
3
- import gradio as gr # Changed from streamlit to gradio
 
4
 
5
- # ─── 1. Load environment variables ─────────────────────────────────────────
6
  load_dotenv()
7
  COHERE_API_KEY = os.getenv("COHERE_API_KEY")
8
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
9
 
10
  if not COHERE_API_KEY or not GEMINI_API_KEY:
11
- raise ValueError("❗️ Missing COHERE_API_KEY or GEMINI_API_KEY in environment")
 
12
 
13
- # ─── 2. Initialize vector store and embedder clients ───────────────────────
14
  import cohere
15
  import chromadb
16
  from google import genai
17
  from google.genai import types
18
 
19
- # Cohere client for embeddings
20
  co = cohere.Client(COHERE_API_KEY)
21
 
22
- # Gemini client for generation
23
  genai_client = genai.Client(api_key=GEMINI_API_KEY)
24
 
25
- # Chroma vector store client
26
  client = chromadb.Client()
27
 
28
- # Create or get existing collection
29
  collection = client.get_or_create_collection(name="inha-well", embedding_function=None)
30
 
31
- # ─── 3. Ingestion & Embedding (run only once) ──────────────────────────────
32
- # Check if collection is empty to avoid re-ingesting on each run
33
  total_docs = collection.count() if hasattr(collection, 'count') else len(collection.get()['documents'])
34
 
35
  if total_docs == 0:
36
  content_chunks = []
37
  for i in range(1, 4):
38
- # Build the absolute path to each docs folder
39
  folder_path = os.path.join(os.getcwd(), "docs", f"p0000{i}")
40
 
41
- # Add error handling for missing folders
42
  if not os.path.exists(folder_path):
43
  print(f"Warning: Folder {folder_path} not found")
44
  continue
@@ -63,7 +62,6 @@ if total_docs == 0:
63
  embeddings=embeddings
64
  )
65
 
66
- # ─── 4. Retrieval & Prompt Utilities ────────────────────────────────────────
67
  def retrieve_context(question, collection, top_k=2):
68
  qr = co.embed(
69
  texts=[question],
@@ -79,7 +77,7 @@ def get_prompt_plain(context: str, question: str) -> str:
79
  <<START>>
80
  You are a responsible person for answering Inha University (South Korea) information. Using the context below, answer within 300 tokens.
81
  Provide concise, well-structured, answer-oriented responses using markdown formatting for better readability.
82
- Use bullet points, bold text, and proper formatting to make the information clear and easy to read.
83
  Do not repeat the prompt text in your output.
84
 
85
  Context:
@@ -101,14 +99,14 @@ def generate_agent_answer(context: str, question: str) -> str:
101
  stop_sequences=["<<END>>", "<<START>>"]
102
  )
103
  )
104
- # Keep the markdown formatting for nice display
105
  return response.text.strip()
106
 
107
  def rag_answer(question: str, collection) -> str:
108
  context = retrieve_context(question, collection, top_k=2)
109
  return generate_agent_answer(context, question)
110
 
111
- # ─── 5. Gradio Interface Function ─────────────────────────────────────────────
112
  def answer_question(question):
113
  """
114
  Main function that processes the question and returns the answer
@@ -122,33 +120,42 @@ def answer_question(question):
122
  except Exception as e:
123
  return f"Sorry, I encountered an error: {str(e)}"
124
 
125
- # ─── 6. Gradio Frontend ─────────────────────────────────────────────────────
126
- # Create the Gradio interface
127
- demo = gr.Interface(
 
 
 
 
 
 
 
 
 
 
128
  fn=answer_question,
129
- inputs=gr.Textbox(
 
130
  label="Ask me anything about Inha University SGCS…",
131
- placeholder="e.g. How many Major Required credits should I take for graduation? ",
132
  lines=2
133
  ),
134
- outputs=gr.Markdown(
135
- label="πŸ“Œ Answer",
136
- show_copy_button=True
137
- ),
138
  title="πŸ“š Inha University SGCS Info Assistant",
139
- description="Get answers to your questions about Inha University SGCS .",
140
  theme=gr.themes.Soft(),
141
  examples=[
142
- ["What classes should I normally take as 3nd semester ISE student?"],
143
- ["Tell me about student organizations and activities"],
144
- ["What percentage scholarship could I recieve with IELTS 7.0"]
145
  ]
146
  )
147
 
148
- # Launch the app
 
 
149
  if __name__ == "__main__":
150
  demo.launch(
151
- share=True, # Creates a public link
152
- server_name="0.0.0.0", # Allows external access
153
 
154
  )
 
1
  import os
2
  from dotenv import load_dotenv
3
+ import gradio as gr
4
+
5
 
 
6
  load_dotenv()
7
  COHERE_API_KEY = os.getenv("COHERE_API_KEY")
8
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
9
 
10
  if not COHERE_API_KEY or not GEMINI_API_KEY:
11
+ raise ValueError("COHERE_API_KEY or GEMINI_API_KEY is missing")
12
+
13
 
 
14
  import cohere
15
  import chromadb
16
  from google import genai
17
  from google.genai import types
18
 
19
+
20
  co = cohere.Client(COHERE_API_KEY)
21
 
22
+
23
  genai_client = genai.Client(api_key=GEMINI_API_KEY)
24
 
25
+
26
  client = chromadb.Client()
27
 
28
+
29
  collection = client.get_or_create_collection(name="inha-well", embedding_function=None)
30
 
31
+
 
32
  total_docs = collection.count() if hasattr(collection, 'count') else len(collection.get()['documents'])
33
 
34
  if total_docs == 0:
35
  content_chunks = []
36
  for i in range(1, 4):
37
+
38
  folder_path = os.path.join(os.getcwd(), "docs", f"p0000{i}")
39
 
40
+
41
  if not os.path.exists(folder_path):
42
  print(f"Warning: Folder {folder_path} not found")
43
  continue
 
62
  embeddings=embeddings
63
  )
64
 
 
65
  def retrieve_context(question, collection, top_k=2):
66
  qr = co.embed(
67
  texts=[question],
 
77
  <<START>>
78
  You are a responsible person for answering Inha University (South Korea) information. Using the context below, answer within 300 tokens.
79
  Provide concise, well-structured, answer-oriented responses using markdown formatting for better readability.
80
+ Use bullet points, bold text, and proper formatting to make the information interactive, clear and easy to read.
81
  Do not repeat the prompt text in your output.
82
 
83
  Context:
 
99
  stop_sequences=["<<END>>", "<<START>>"]
100
  )
101
  )
102
+
103
  return response.text.strip()
104
 
105
  def rag_answer(question: str, collection) -> str:
106
  context = retrieve_context(question, collection, top_k=2)
107
  return generate_agent_answer(context, question)
108
 
109
+
110
  def answer_question(question):
111
  """
112
  Main function that processes the question and returns the answer
 
120
  except Exception as e:
121
  return f"Sorry, I encountered an error: {str(e)}"
122
 
123
+
124
+ import gradio as gr
125
+
126
+ def answer_question(message, history):
127
+ history = history + [[message, ""]]
128
+
129
+ # Replace this with your real logic / LLM call
130
+ response = "Here is the answer about SGCS..." # ← Dummy response
131
+ history[-1][1] = response
132
+ return history, history
133
+
134
+ # Define interface as chatbot-style inside Interface-like wrapper
135
+ chat_interface = gr.ChatInterface(
136
  fn=answer_question,
137
+ chatbot=gr.Chatbot(label="πŸ“š Inha University SGCS Info Assistant"),
138
+ textbox=gr.Textbox(
139
  label="Ask me anything about Inha University SGCS…",
140
+ placeholder="e.g. How many Major Required credits should I take for graduation?",
141
  lines=2
142
  ),
 
 
 
 
143
  title="πŸ“š Inha University SGCS Info Assistant",
144
+ description="Get answers to your questions about Inha University SGCS.",
145
  theme=gr.themes.Soft(),
146
  examples=[
147
+ "What classes should I normally take as 3rd semester ISE student?",
148
+ "Tell me about student organizations and activities",
149
+ "What percentage scholarship could I receive with IELTS 7.0"
150
  ]
151
  )
152
 
153
+ demo = chat_interface
154
+
155
+
156
  if __name__ == "__main__":
157
  demo.launch(
158
+ share=True,
159
+ server_name="0.0.0.0",
160
 
161
  )