iAIChat commited on
Commit
b0766d8
1 Parent(s): af4642f

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. README.md +3 -9
  2. gradio_exit.py +97 -0
  3. requirements.txt +6 -0
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Gradio Text Summary App
3
- emoji: 📊
4
- colorFrom: indigo
5
- colorTo: purple
6
  sdk: gradio
7
- sdk_version: 3.43.2
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Gradio_Text_Summary_App
3
+ app_file: gradio_exit.py
 
 
4
  sdk: gradio
5
+ sdk_version: 3.38.0
 
 
6
  ---
 
 
gradio_exit.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #https://stackoverflow.com/questions/75286784/how-do-i-gracefully-close-terminate-gradio-from-within-gradio-blocks
2
+ #https://levelup.gitconnected.com/bringing-your-ai-models-to-life-an-introduction-to-gradio-ae051ca83edf
3
+ #Bringing Your AI Models to Life: An Introduction to Gradio - How to demo your ML model quickly without any front-end hassle.
4
+ #pipreqs . --encoding=utf-8
5
+
6
+ #https://huggingface.co/blog/inference-endpoints
7
+ #Getting Started with Hugging Face Inference Endpoints
8
+
9
+ #https://www.gradio.app/guides/using-hugging-face-integrations
10
+ Using Hugging Face Integrations
11
+
12
+ #在app.py的根目录下cmd命令行窗口中运行:gradio deploy,将会出现如下提示:
13
+ #To login, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens .
14
+ #Token can be pasted using 'Right-Click'.
15
+ #Token:先将HF_API_TOKEN拷贝到剪贴板之后,邮件单击即可
16
+ #Add token as git credential? (Y/n) n
17
+ #Token is valid (permission: write).
18
+ #Your token has been saved to C:\Users\lenovo\.cache\huggingface\token
19
+ #Login successful
20
+ #Creating new Spaces Repo in 'D:\ChatGPTApps\Gradio_HF_Apps'. Collecting metadata, press Enter to accept default value.
21
+ #Enter Spaces app title [Gradio_HF_Apps]:
22
+
23
+
24
+ import os
25
+ import io
26
+ import requests
27
+ import json
28
+ from IPython.display import Image, display, HTML
29
+ from PIL import Image
30
+ import base64
31
+ from dotenv import load_dotenv, find_dotenv
32
+
33
+ _ = load_dotenv(find_dotenv()) # read local .env file
34
+ hf_api_key ="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"
35
+
36
+ #model_id = "sentence-transformers/all-MiniLM-L6-v2"
37
+ model_id = "shleifer/distilbart-cnn-12-6"
38
+ #hf_token = "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"
39
+
40
+ #ENDPOINT_URL='https://api-inference.huggingface.co/models/DunnBC22/flan-t5-base-text_summarization_data'
41
+ api_url ='https://api-inference.huggingface.co/models/DunnBC22/flan-t5-base-text_summarization_data'
42
+ #api_url = f"https://api-inference.huggingface.co/pipeline/feature-extraction/{model_id}"
43
+ #pipeline的api-inference/Endpoint是对应于transformers(用于生成向量Embeddings)的!
44
+ #headers = {"Authorization": f"Bearer {hf_token}"}
45
+ headers = {"Authorization": f"Bearer {hf_api_key}"}
46
+
47
+ #def query(texts):
48
+ # response = requests.post(api_url, headers=headers, json={"inputs": texts, "options":{"wait_for_model":True}})
49
+ # return response.json()
50
+
51
+ #可以不使用ENDPOINT_URL,而是使用api_url(或者两个是一回事?):https://huggingface.co/blog/getting-started-with-embeddings
52
+ def get_completion(inputs, parameters=None, ENDPOINT_URL=api_url):
53
+ headers = {
54
+ "Authorization": f"Bearer {hf_api_key}",
55
+ "Content-Type": "application/json"
56
+ }
57
+ data = { "inputs": inputs }
58
+ if parameters is not None:
59
+ data.update({"parameters": parameters})
60
+ response = requests.request("POST",
61
+ ENDPOINT_URL, headers=headers,
62
+ data=json.dumps(data)
63
+ )
64
+ return json.loads(response.content.decode("utf-8"))
65
+
66
+ text = (
67
+ '''Alexander III of Macedon (Ancient Greek: Ἀλέξανδρος, romanized: Alexandros; 20/21 July 356 BC – 10/11 June 323 BC), '''
68
+ '''commonly known as Alexander the Great,[a] was a king of the ancient Greek kingdom of Macedon.[a] '''
69
+ '''He succeeded his father Philip II to the throne in 336 BC at the age of 20, '''
70
+ '''and spent most of his ruling years conducting a lengthy military campaign throughout Western Asia and Egypt. '''
71
+ '''By the age of 30, he had created one of the largest empires in history, stretching from Greece to northwestern India.[2] '''
72
+ '''He was undefeated in battle and is widely considered to be one of history's greatest and most successful military commanders.[3][4]'''
73
+ )
74
+
75
+ get_completion(text) #api_url中的model_id = "sentence-transformers/all-MiniLM-L6-v2"时,返回的结果是浮点数(即向量Embeddings)
76
+
77
+ import gradio as gr
78
+
79
+ def summarize(input):
80
+ output = get_completion(input)
81
+ return output[0]['summary_text']
82
+
83
+ gr.close_all()
84
+
85
+ demo = gr.Interface(fn=summarize,
86
+ inputs=[gr.Textbox(label="Text to summarize", lines=6)],
87
+ outputs=[gr.Textbox(label="Result", lines=3)],
88
+ title="Text summarization with distilbart-cnn",
89
+ description="Summarize any text using the `shleifer/distilbart-cnn-12-6` model under the hood!"
90
+ )
91
+
92
+ #demo.launch(share=True, server_port=int(os.environ['PORT2']))
93
+ #demo.launch(share=True)
94
+ demo.launch()
95
+
96
+
97
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio==3.43.2
2
+ ipython==8.14.0
3
+ Pillow==9.5.0
4
+ Pillow==10.0.0
5
+ python-dotenv==1.0.0
6
+ Requests==2.31.0