ghus75 commited on
Commit
f254bad
·
1 Parent(s): 33cc6a6

use HF token

Browse files
Files changed (2) hide show
  1. app.py +40 -4
  2. requirements.txt +2 -0
app.py CHANGED
@@ -1,8 +1,44 @@
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
+ from langchain_core.messages import HumanMessage, SystemMessage
4
+ from langchain_huggingface import HuggingFaceEndpoint, ChatHuggingFace
5
 
6
+ import os
7
+ HF_Hub_API_token = os.environ.get('HF_Hub_API_token', None)
8
+
9
+ llm = HuggingFaceEndpoint(
10
+ repo_id="mistralai/Mistral-7B-Instruct-v0.2",
11
+ task="text-generation",
12
+ max_new_tokens=128,
13
+ do_sample=False,
14
+ repetition_penalty=1.03,
15
+ huggingfacehub_api_token=HF_Hub_API_token
16
+ )
17
+
18
+ chat_model = ChatHuggingFace(llm=llm)
19
+
20
+ MAX_LEN = 100
21
+ def gen_prompt1(title):
22
+ prompt1 = title
23
+ formatted_str = ''
24
+ if len(title) > 0:
25
+ messages = [HumanMessage(content=prompt1),]
26
+ res = chat_model.invoke(messages)
27
+ # censure
28
+ for s in res.content.split('\n'):
29
+ if len(s) > MAX_LEN:
30
+ s = s[:MAX_LEN] + " (...)"
31
+ formatted_str += s + '\n'
32
 
33
+ return formatted_str #res.content
34
+
35
+ example_title = "Mary had a little lamb"
36
+
37
+ demo = gr.Interface(
38
+ fn=gen_prompt1,
39
+ inputs=gr.Textbox(label = "Introduction", show_label=True),
40
+ outputs=["text"],
41
+ examples=[example_title]
42
+ )
43
+
44
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ langchain-core
2
+ langchain-huggingface