vishalkatheriya commited on
Commit
20ac504
·
verified ·
1 Parent(s): 9d50c04

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from googlesearch import search
4
+
5
+ # Function to load the model
6
+ @st.cache_resource
7
+ def load_model():
8
+ model_name = "nisten/Biggie-SmoLlm-0.15B-Base" # Replace with your preferred model
9
+ generator = pipeline("text-generation", model=model_name)
10
+ return generator
11
+
12
+ # Load the model once
13
+ generator = load_model()
14
+
15
+ # Function to process the query using the open-source LLM for general chat
16
+ def chat_with_llm(query):
17
+ prompt = f"User: {query}\nAssistant:"
18
+ response = generator(prompt, max_length=100, num_return_sequences=1)
19
+ return response[0]['generated_text'].strip()
20
+
21
+ # Function to process the query for search intent
22
+ def process_query_with_llm(query):
23
+ prompt = f"User asked: '{query}'. What would be the best search query to use?"
24
+ response = generator(prompt, max_length=50, num_return_sequences=1)
25
+ return response[0]['generated_text'].strip()
26
+
27
+ # Function to perform a Google search using the googlesearch-python package
28
+ def search_web(query):
29
+ search_results = []
30
+ for result in search(query, num_results=3):
31
+ search_results.append(result)
32
+ return search_results
33
+
34
+ # Streamlit UI
35
+ st.title("Interactive Chatbot")
36
+
37
+ # Input field for user query
38
+ user_input = st.text_input("You:", "")
39
+
40
+ if user_input:
41
+ st.write(f"**You:** {user_input}")
42
+
43
+ # Determine if the query is a search or a general chat
44
+ if any(keyword in user_input.lower() for keyword in ["search", "find", "get me", "give me"]):
45
+ # If the user input indicates a search intent
46
+ search_query = process_query_with_llm(user_input)
47
+ st.write(f"**Processed Query:** {search_query}")
48
+
49
+ # Search the web using the processed query
50
+ links = search_web(search_query)
51
+
52
+ # Display the search results
53
+ if links:
54
+ st.write("Here are some links you might find useful:")
55
+ for idx, link in enumerate(links):
56
+ st.write(f"{idx + 1}. {link}")
57
+ else:
58
+ st.write("Sorry, I couldn't find any relevant links.")
59
+ else:
60
+ # Handle general conversation
61
+ response = chat_with_llm(user_input)
62
+ st.write(f"**Chatbot:** {response}")