mou3az commited on
Commit
6dacbc2
1 Parent(s): 0394b1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -3
app.py CHANGED
@@ -1,7 +1,73 @@
1
  import gradio as gr
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  demo.launch()
 
1
  import gradio as gr
2
+ from langchain.embeddings import SentenceTransformerEmbeddings
3
+ from langchain.vectorstores import FAISS
4
+ from langchain_community.chat_models.huggingface import ChatHuggingFace
5
+ from langchain.schema import SystemMessage, HumanMessage, AIMessage
6
+ from langchain_community.llms import HuggingFaceEndpoint
7
 
8
+ model_name = "sentence-transformers/all-mpnet-base-v2"
9
+ embedding_llm = SentenceTransformerEmbeddings(model_name=model_name)
10
 
11
+ db = FAISS.load_local("faiss_index", embedding_llm, allow_dangerous_deserialization=True)
12
+
13
+ # Set up Hugging Face model
14
+ llm = HuggingFaceEndpoint(
15
+ repo_id="HuggingFaceH4/starchat2-15b-v0.1",
16
+ task="text-generation",
17
+ max_new_tokens=4096,
18
+ temperature=0.6,
19
+ top_p=0.9,
20
+ top_k=40,
21
+ repetition_penalty=1.2,
22
+ do_sample=True,
23
+ )
24
+ chat_model = ChatHuggingFace(llm=llm)
25
+
26
+ messages = [
27
+ SystemMessage(content="You are a helpful assistant."),
28
+ HumanMessage(content="Hi AI, how are you today?"),
29
+ AIMessage(content="I'm great thank you. How can I help you?")
30
+ ]
31
+
32
+ def handle_query(mode: str, query: str):
33
+ if mode == "chat":
34
+ return chat_mode(query)
35
+ elif mode == "web-search":
36
+ return web_search(query)
37
+ else:
38
+ return "Invalid mode selected."
39
+
40
+ def chat_mode(query: str):
41
+ prompt = HumanMessage(content=query)
42
+ messages.append(prompt)
43
+ response = chat_model.invoke(messages)
44
+ messages.append(response.content)
45
+ if len(messages) >= 6:
46
+ messages = messages[-6:]
47
+ return f"You: {query}\nIT-Assistant: {response.content}"
48
+
49
+ def web_search(query: str):
50
+ similar_docs = db.similarity_search(query, k=3)
51
+ if similar_docs:
52
+ source_knowledge = "\n".join([x.page_content for x in similar_docs])
53
+ else:
54
+ source_knowledge = ""
55
+ augmented_prompt = f"""
56
+ Answer the next query using the provided Web Search.
57
+ If the answer is not contained in the Web Search, ignore the web search and respond independently with your own knowledge.
58
+
59
+ Query: {query}
60
+
61
+ Web Search:
62
+ {source_knowledge}
63
+ """
64
+ prompt = HumanMessage(content=augmented_prompt)
65
+ messages.append(prompt)
66
+ response = chat_model.invoke(messages)
67
+ messages.append(response.content)
68
+ if len(messages) >= 6:
69
+ messages = messages[-6:]
70
+ return f"You: {query}\nIT-Assistant: {response.content}"
71
+
72
+ demo = gr.Interface(fn=handle_query, inputs=["text", "text"], outputs="text", title="IT Assistant", description="Choose a mode and enter your message, then click submit to interact.", inputs_layout="vertical", choices=["chat", "web-search"])
73
  demo.launch()