sarthakrw commited on
Commit
a0bfec6
·
1 Parent(s): f601027

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, pipeline
2
+ from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig
3
+
4
+ model_name = "TheBloke/Llama-2-7b-Chat-GPTQ"
5
+ model_basename = "model"
6
+
7
+ use_triton = False
8
+
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
10
+
11
+ model = AutoGPTQForCausalLM.from_quantized(model_name,
12
+ use_safetensors=True,
13
+ trust_remote_code=True,
14
+ device="cuda:0",
15
+ use_triton=use_triton,
16
+ quantize_config=None)
17
+
18
+ #creating pipeline
19
+ pipe = pipeline(
20
+ "text-generation",
21
+ model=model,
22
+ tokenizer=tokenizer,
23
+ max_new_tokens=512,
24
+ temperature=0.6,
25
+ top_p=0.95,
26
+ repetition_penalty=1.15
27
+ )
28
+
29
+ from langchain.llms import HuggingFacePipeline
30
+ from langchain.chains import LLMChain
31
+ from langchain import PromptTemplate
32
+
33
+ llm = HuggingFacePipeline(pipeline=pipe)
34
+
35
+ system_message = """
36
+ You are a helpful, respectful and honest assistant. Your job is to answer the users query as best as possible given the Web Page Content. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. If you DO NOT KNOW THE ANSWER DO NOT SHARE FALSE INFORMATION.
37
+ You have been given scraped text content of a webpage under the section called "Web Page Content". Using this information answer the users query. However, if the webpage DOES NOT contain the answer to the query, you CAN answer based on your existing knowledge IF you are sure of the answer, but ALWAYS let the user know when doing so.
38
+ """
39
+
40
+ prompt_template='''[INST] <<SYS>>
41
+ {system_message}
42
+ <</SYS>>
43
+
44
+ Web Page Content:
45
+ ```
46
+ {context}
47
+ ```
48
+
49
+ {prompt} [/INST]'''
50
+
51
+
52
+ chat = LLMChain(
53
+ llm=llm,
54
+ prompt=PromptTemplate.from_template(prompt_template),
55
+ verbose=True
56
+ )
57
+
58
+ import requests
59
+ from bs4 import BeautifulSoup
60
+ import re
61
+
62
+ def run(url, input):
63
+ context = scraper(url)
64
+ response = chat.predict(system_message=system_message, context=context, prompt=input)
65
+
66
+ return response
67
+
68
+ import gradio as gr
69
+
70
+ # Create a Gradio interface
71
+ iface = gr.Interface(
72
+ fn=run,
73
+ inputs=["text","text"],
74
+ outputs="text",
75
+ title="Web Query App",
76
+ description="Enter the webpage url and your query\nIMPORTANT: Larger webpages are likely to cause error due to lack of computational resources"
77
+ )
78
+
79
+ # Launch the interface
80
+ iface.launch(inline=False)